Objective c 删除并添加UITableView行

Objective c 删除并添加UITableView行,objective-c,uitableview,ios7,Objective C,Uitableview,Ios7,在我的应用程序中,我在-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)节中指定了一个UITableView,以便有150行。现在,我的数据源NSArray有150多个元素,因此,正如预期的那样,我的表显示了前150个元素。我还实现了-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)ed

在我的应用程序中,我在
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)节中指定了一个UITableView,以便有150行。现在,我的数据源NSArray有150多个元素,因此,正如预期的那样,我的表显示了前150个元素。我还实现了
-(void)tableView:(UITableView*)tableView提交的编辑样式:(UITableViewCellEditingStyle)editingStyle for rowatindexpath:(nsindepath*)indepath
,以便用户可以从UITableView中删除元素。当用户删除一行时,我想让删除的行消失(带有动画),然后让其余的行向上滑动,并将数据源数组的第151个元素(以前未显示)显示为表的最后一个元素。然而,迄今为止我所尝试的一切都给了我以下错误:

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 (150) must be equal to the number of rows contained in that section before the update (150), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
First throw call stack:
(0x2e58fecb 0x38d2ace7 0x2e58fd9d 0x2ef3de2f 0x30f94761 0x30ff0f7f 0x2af0f49 0xdf763 0x30fba567 0x30fba4f9 0x30df66a7 0x30df6643 0x30df6613 0x30de1d5b 0x30df605b 0x30db9521 0x30df1305 0x30df0c2b 0x30dc5e55 0x30dc4521 0x2e55afaf 0x2e55a477 0x2e558c67 0x2e4c3729 0x2e4c350b 0x334326d3 0x30e24871 0x106b59 0x39228ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
下面的代码解释了我如何实现这一点的要点:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        PNObject *objectToDelete = contentArray[indexPath.row];
        NSMutableArray *mutableCopy = [contentArray mutableCopy];
        [mutableCopy removeObject:objectToDelete];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

不知道是否仍然相关,
但我认为现在发生的是,您实际上并没有更新数据源

您正在创建数据源的“本地”可变副本,并删除该副本中的对象,但实际数据源保持不变,并且仍然包含要删除的项

我猜您的
numberOfRows:instation:
包含了
[contentArray count]

因此,发生的情况是,您正在从表视图中“删除”一个项目,因此您的应用程序会将该表视图之外的项目减少一个,
但在重新加载表视图的内容时,由于该项没有从数据源中删除,表视图比预期多了一个项

要解决此问题,请将contentArray更改为NSMutableArray,
在上面的代码中,直接从那里删除
objectToDelete

然后还可以直接从数组中删除对象,而无需创建对该对象的本地引用。 因此,上面的代码应该如下所示:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {  

        [contentArray removeObjectAtIndex:indexPath.row];  
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
    }  
}  

已经解决了这个问题,但我还是会给你答案的。谢谢。是的,我想你现在已经找到了解决办法。无论如何,感谢您将其标记为答案。