ios '无效更新:第 0 节中的行数无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21870680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'Invalid update: invalid number of rows in section 0
提问by user3085646
I've read all of the related posts regarding this and I am still having an error:
我已经阅读了所有与此相关的帖子,但仍然有错误:
'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).'
Here are the details:
以下是详细信息:
in .h
I have an NSMutableArray
:
在.h
我有一个NSMutableArray
:
@property (strong,nonatomic) NSMutableArray *currentCart;
In .m
my numberOfRowsInSection
looks like this:
在.m
我numberOfRowsInSection
看来是这样的:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return ([currentCart count]);
}
To enable delete and remove the object from the array:
要启用从数组中删除和移除对象:
// 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];
}
}
I thought that by having my number of sections relying on the count of the array I'm editing it would ensure the proper number of rows? Can't this be done without having to reload the table when you delete a row anyway?
我认为通过让我的部分数量依赖于我正在编辑的数组的计数,它会确保正确的行数吗?无论如何,在删除行时不必重新加载表就不能完成此操作吗?
回答by Undo
You need to remove the object from your data array beforeyou call deleteRowsAtIndexPaths:withRowAnimation:
. So, your code should look like this:
您需要在调用之前从数据数组中删除该对象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];
}
}
You can also simplify your code a little by using the array creation shortcut @[]
:
您还可以使用数组创建快捷方式稍微简化您的代码@[]
:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
回答by Alejandra Vidal Ortiz
Swift Version --> Remove the object from your data array before you call
Swift 版本 --> 在调用之前从数据数组中删除对象
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)
}
}
回答by Haseeb Iqbal
In my case issue was that numberOfRowsInSection
was returning similar number of rows after calling tableView.deleteRows(...)
.
就我而言,问题是numberOfRowsInSection
在调用tableView.deleteRows(...)
.
Since this was the required behaviour in my case, I ended up calling tableView.reloadData()
instead of tableView.deleteRows(...)
in cases where numberOfRowsInSection
will remain same after deleting a row.
由于在我的情况下这是必需的行为,因此我最终调用tableView.reloadData()
而不是在删除行后将保持不变的tableView.deleteRows(...)
情况下调用numberOfRowsInSection
。
回答by 7RedBits.com
Here is some code from above added with actual action code (point 1 and 2);
这是上面添加的一些代码,并添加了实际操作代码(第 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
}