2012-01-18 35 views
0

我有一个tableView使用NSFetchedResultsController来填充viewDidLoad上的表格单元格。用户可以使用UISegmentControl(index 0 ='number',index 1 ='name')对tableView进行排序。NSFetchedResultsController didChangeObject排序后使用了错误的indexPath

当用户单击UISegmentControl时,它调用函数sortShortListUsingSelectedSortIndex,此函数将使用setSortDescriptorsNSFetchedResultsController重新排序tableView。当表格被重新排序并且用户选择一行时,didSelectRowAtIndexPath:显示我期望看到的NSIndexPath(例如,IndexPath [0,6]被重新排序为Index [0,3]),但是当试图删除相同的排在NSIndexPathdidChangeObject:atIndexPath:forChangeType:newIndexPath显示旧IndexPath [0,6]不[0,3],并会导致崩溃:

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (9), 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). with userInfo (null)

这里是我的方法的简化版本:Gist

我为什么在我的tableView中看到同一行的不同索引?

回答

0

此问题的解决方法是确保我的提取控制器在尝试重新分配之前正确释放。这就是indexPath使用旧索引的原因。

[fetchRequestController release]; 
fetchRequestController = [[NSFetchedResultsController alloc] initWithFetchRequest:shortListRequest 
                  managedObjectContext:context 
                   sectionNameKeyPath:nil 
                     cacheName:nil]; 
相关问题