1

我有一个 tableView 显示一个NSMutableArray对象,我目前正在尝试实现该tableView:commitEditingStyle:forRowAtIndexPath:方法。如果我注释掉该行[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];,那么除了更新 UI 之外,一切正常。

这是我对该方法的实现:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.modelClass removeObjectAtRow:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

这是我得到的错误:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2280.1/UITableView.m:1060

其他问题的答案表明我需要将 替换为@[indexPath][NSArray arrayWithObject:indexPath]而这只是出现了同样的错误。

4

2 回答 2

3

可能是方法中的问题numberOfRowsInSection所以,要留下麻烦,您应该:

在函数commitEditingStyle中从您的数组、数据库等中删除数据。减少您当前的行数。

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

你的问题就解决了!!!

于 2012-07-02T05:12:30.620 回答
2

这是因为在删除行之后再次UITableView进入数据源方法。numberOfRowsInSection确保它返回正确的行数,即您的self.modelClass.countin numberOfRowsInSection

于 2012-07-02T05:12:24.553 回答