gpt4 book ai didi

iphone - 删除行时崩溃

转载 作者:行者123 更新时间:2023-11-29 04:37:58 25 4
gpt4 key购买 nike

在我的 TableView 类中使用以下方法,我试图删除 TableView 的一行

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrUserData count];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}

- (void)tableView:(UITableView *)tv commitEditingStyle (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[arrUserData removeObjectAtIndex:indexPath.row];
}
}

表中有大约 40 行,当我单击任何行上的红色删除按钮时,它会在该行崩溃

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

具有以下崩溃日志

2012-05-30 14:58:45.835 testDeleteRow[3276:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (41) must be equal to the number of rows contained in that section before the update (41), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

谁能告诉我如何解决这个问题,提前谢谢

最佳答案

实际上,当您删除单元格时,您需要更新数据源。如果您使用array来填充table,则在此行之后 -

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

您需要从数组中删除同一索引处的对象。此外,section 中的行数应减少 1。如果您在那里返回 array 计数,那么它将被自动处理。

关于iphone - 删除行时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10814076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com