我已经阅读了所有与此相关的帖子,但我仍然有一个错误:
'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), 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).'
以下是详细情况:
我的.h
分是NSMutableArray
分:
@property (strong,nonatomic) NSMutableArray *currentCart;
在.m
,我的numberOfRowsInSection
看起来是这样的:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return ([currentCart count]);
}
要启用删除并从数组中删除对象,请执行以下操作:
// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//when delete is tapped
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[currentCart removeObjectAtIndex:indexPath.row];
}
}
我认为,通过让我的区段数依赖于我正在编辑的数组的计数,可以确保适当的行数?在删除行时,不需要重新装入表就可以做到这一点吗?