这个问题使我在最近几个小时都很忙。我有两个部分,每个部分一行。当我删除其中一个部分中的行时,它抛出一个异常,说这是无效的更新(更新前后的行/部分数不同)。这是可以理解的,因为我删除了部分的最后一行,因此删除了该部分。问题是如何避免异常。
我的数据源一切正常。我检查并重新检查(相信我)。
因此,作为线程状态的标题,如何在没有异常的情况下删除节的最后一行?
谢谢,
Bart
[删除一行时,该行是该节的最后一个,则还需要删除该节。基本上,您需要既要跟踪与行关联的要删除的indexPath,又要跟踪与要删除的节相关的索引,因为它们不再包含行。您可以按照以下步骤进行操作:
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];
每次从模型数组中删除与tableView的特定部分相关的对象时,请检查数组计数是否为零,在这种情况下,将代表该部分的索引添加到索引中:
[array removeObjectAtIndex:indexPath.row];
if(![array count])
[indexes addIndex: indexPath.section];
确定与要删除的行相关的所有indexPath,然后按如下所示更新tableView:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
这对我和我建议的其他人都有用。
也许这可能对您有用:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [self.keys objectAtIndex:section];
NSMutableArray *rowsInSection = [self.dict objectForKey:key];
[rowsInSection removeObjectAtIndex:row];
NSUInteger rowsInSectionCount = [rowsInSection count];
if (rowsInSectionCount > 0) {
// If we still have rows in this section just delete the row
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else {
// There are no more rows in this section
[self.dict removeObjectForKey:key];
[self.keys removeObjectAtIndex:section];
NSUInteger sectionCount = [self.keys count];
if (sectionCount > 0) {
// If we still have 1 or more sections left just delete this section
[tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}
else {
// If there are no more rows and sections left just delete the last row
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}
}
}
希望这就是您想要的。
这应该可以解决问题:
// Either delete some rows within a section (leaving at least one) or the entire section.
if ([indexPath section] < 0) {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if ([indexPath section] == 0) {
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
}
对于Swift 2,在删除模型中的数据之后:
tableView.beginUpdates()
let indexSet = NSMutableIndexSet()
indexSet.addIndex(indexPath.section)
tableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Fade)
tableView.endUpdates()
删除部分的最后一行,然后需要删除该部分
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .delete:
if tableView.numberOfRows(inSection: indexPath!.section) > 1 {
tableView.deleteRows(at: [indexPath!], with: .automatic)
} else {
let section = indexPath!.section
let indexSet = IndexSet(integer: section)
tableView.deleteSections(indexSet, with: .automatic)
}
default:
break
}
}