’无效更新: 第0部分中的行数无效

我已经阅读了所有与此相关的文章,但仍然有一个错误:

'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];




}
}

我认为,通过我的节数依赖于数组的计数,我正在编辑它将确保适当的行数?当您删除一行时,不必重新加载表就可以完成这个操作吗?

121882 次浏览

您需要从您的数据数组 之前中删除您调用 deleteRowsAtIndexPaths:withRowAnimation:的对象。因此,您的代码应该如下所示:

// Editing of rows is enabled
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {


//when delete is tapped
[currentCart removeObjectAtIndex:indexPath.row];


[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

您还可以使用数组创建快捷方式 @[]来简化代码:

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

调用之前从数据数组中删除对象

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("Deleted")


currentCart.remove(at: indexPath.row) //Remove element from your array
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}

在我的案例中,问题是 numberOfRowsInSection在调用 tableView.deleteRows(...)之后返回相似数量的行。

因为在我的例子中这是必需的行为,所以在 numberOfRowsInSection在删除一行之后仍然保持不变的情况下,我最终调用 tableView.reloadData()而不是 tableView.deleteRows(...)

下面是一些从上面添加了实际行动代码(点1和2)的代码;

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { _, _, completionHandler in


// 1. remove object from your array
scannedItems.remove(at: indexPath.row)
// 2. reload the table, otherwise you get an index out of bounds crash
self.tableView.reloadData()


completionHandler(true)
}
deleteAction.backgroundColor = .systemOrange
let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = true
return configuration
}