tableView:cellForRowAtIndexPath 删除项目后用 nil indexPath 调用 [英] tableView:cellForRowAtIndexPath called with nil indexPath after deleting item
问题描述
我有一个由 NSFetchedResultsController
管理的相当普通的 UITableView
来显示给定核心数据实体的所有实例.
I have a rather vanilla UITableView
managed by an NSFetchedResultsController
to display all instances of a given Core Data entity.
当用户通过滑动删除表格视图中的条目时,tableView:cellForRowAtIndexPath:
最终会在我的 UITableViewController
上被调用,并带有 nil代码> <代码>索引路径代码>.由于我没想到它会用
nil
indexPath
调用,因此应用程序崩溃了.
When the user deletes an entry in the table view by swiping over it, tableView:cellForRowAtIndexPath:
eventually gets called on my UITableViewController
with a nil
indexPath
. Since I had not expected it to be called with a nil
indexPath
, the app crashes.
我可以通过检查 nil
值然后返回一个空单元格来解决崩溃问题.这似乎有效,但我仍然担心我可能处理错误.有任何想法吗?有没有人见过用 nil
indexPath
调用的 tableView:cellForRowAtIndexPath:
?
I can work around the crash by checking for that nil
value and then returning an empty cell. This seems to work, but I still worry that I may have handled something wrong. Any ideas? Has anybody ever seen tableView:cellForRowAtIndexPath:
called with a nil
indexPath
?
请注意,这仅在用户通过在单元格上滑动从表格视图中删除时发生.使用表格视图编辑模式删除项目时,不会发生这种情况.删除单元格的两种方式有什么不同?
Note that this only happens when the user deletes from the table view by swiping over the cell. When deleting an item using the table view editing mode, it doesn't happen. What would be different between the two ways to delete a cell?
那么在表视图委托方法中获得 nil
indexPath
真的是一个好的情况吗?
So is it really an OK situation to get a nil
indexPath
in a table view delegate method?
我的视图控制器代码非常标准.这是删除:
My view controller code is really standard. Here is the deletion:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.moc deleteObject:managedObject];
NSError *error = NULL;
Boolean success = [self.moc save:&error];
if (!success) { <snip> }
// actual row deletion from table view will be handle from Fetched Result Controller delegate
// [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else { <snip> }
}
这将导致调用 NSFetchedResultsController
委托方法:
This will lead to the NSFetchedResultsController
delegate method being called:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeInsert: <snip> break;
case NSFetchedResultsChangeUpdate: <snip> break;
case NSFetchedResultsChangeMove: <snip> break;
}
}
当然,数据源方法由 NSFetchedResultsController
处理,例如:
And of course, the data source methods are handled by the NSFetchedResultsController
, e.g.:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
非常感谢.
推荐答案
您似乎要从表中删除 indexPath,但表数据源并未更新.您是否验证了 NSFetchedResultsController 的数据源更新过程是否正确更新了表数据源.?
It seems like you are deleting the indexPath from table but table data source is not updating. Did you verify the data source udation process by NSFetchedResultsController is correctly updationg the table data source.?
这篇关于tableView:cellForRowAtIndexPath 删除项目后用 nil indexPath 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!