我正在尝试使用以下代码删除。
[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
它返回多个异常。
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070
2013-01-29 16:28:22.628
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. 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, 5 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
最佳答案
这是因为您应该有一种返回行数的动态方式。
例如,我创建了 3 个数组。每个都有 3 个值(这些是 NSArray
变量):
在.h
文件中:
NSArray *firstArray;
NSArray *secondArray;
NSArray *thirdArray;
在 .m
文件中,viewDidLoad 或 init 或类似的内容:
firstArray = [NSArray arrayWithObjects:@"Cat", @"Mouse", @"Dog", nil];
secondArray = [NSArray arrayWithObjects:@"Plane", @"Car", @"Truck", nil];
thirdArray = [NSArray arrayWithObjects:@"Bread", @"Peanuts", @"Ham", nil];
当返回表中的行数时,我有:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return array.count;
if (section == 0) {
return firstArray.count;
} else if (section == 1) {
return secondArray.count;
} else {
return thirdArray.count;
}
}
然后,在cellForRow
中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if (indexPath.section == 0) {
cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
} else if (indexPath.section == 1) {
cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
}
return cell;
}
然后我通过在 table 上滑动或您想要删除的其他方式来删除@“Dog”
。然后,当重新加载表格时,数组计数将为 2,因此表格将“知道”它必须仅显示 2 行。基本上,您还需要更新数据源。
它也适用于其他部分。因为您从数组中删除元素,所以行数也会更新。
关于iOS-deleteRowsAtIndexPaths 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14581923/