iOS - PFQueryTableViewController - 删除行崩溃
我使用PFQueryTableViewController与本地数据存储。我想使用户从表中删除对象与此代码:iOS - PFQueryTableViewController - 删除行崩溃
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
PFObject * object = [self.objects objectAtIndex: indexPath.row];
[object deleteInBackgroundWithBlock:^(BOOL succeeded, NSError * error) {
[self loadObjects];
}];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
我得到这个 ***终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“无效的更新:行数无效在部分0.更新(2)后现有部分中包含的行数必须等于更新前(2)部分中包含的行数,加上或减去从该部分插入或删除的行数部分(0插入,1删除)以及正数或负数移入或移出该部分的行数(移入0,移出0)。'
我想,这是一个PFQueryTableViewController中使用的注释,但我找不到解决方案。 非常感谢。
当你从一个数组删除对象,你改变你的UITableView
比方说,你开始在一个阵列100个对象叫objectsArray
结构。
- numberOfRowsInSection =返回objectsArray.count(即100)
- cellsForRowAtIndexPath = 100个细胞,或然而,许多将被重用,以显示所有100个对象
现在你只是删除了一些行从UITableView
这里:[tableView deleteRowsAtIndexPaths:....]
所以我们假设你从objectsArray
中删除了3行,这意味着你从UITableView
中删除了有形的行,所以UITableView
认为numberOfRows..
= 100-3。但它没有,因为你没有更新你的数组减去刚刚删除的那3个对象。
所以,你实际上是重新加载tableView [self loadObjects]
之前那些有形的3行被删除,或可能在你的情况下,因为inBackground
部分。换句话说,在您试图为从tableView中删除的行设置动画之前,您再次加载objectsArray
。这不可能发生得很快,特别是因为你把它放在一个异步回调中,你可能不应该为了性能而这样做。因此,在短期,你需要更新后您的阵列您删除行,以便numberOfRowsInSection
将始终反映对象
的正确数量如果你的数据是敏感的,你需要等待,看是否回调返回成功deleteInBackground
那么你也应该更新你的tableView那里,因为你永远不知道什么时候该方法会实际完成:
..deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
//get main thread
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self loadObjects];
} else {
//error in deleting them
}
}
谢谢你的回答。有了这段代码,我得到了其他错误:***声明失败 - [UITableView _endCellAnimationsWithContext:],/SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:1623我正在调查这个错误的含义。你有什么主意吗。 –
最后我在这里找到了解决方案:http://stackoverflow.com/questions/31358082/when-delete-cell-and-self-loadobjects-in-pfquerytableviewcontroller-it-gets-a –
哦拍。对不起,我的意思是改变这一点,但有其他问题跟踪并忘记回来。抱歉。这个错误是给定的 – soulshined