1
我有一個UITableView
核心數據。deleteRowsAtIndexPaths凍結UITableView
當我插入新記錄或更新現有記錄時,一切進展順利,UITableView
會自動更新,但是當我刪除記錄時,UITableView
凍結,直到UIView
重新加載。
有人知道發生了什麼問題嗎?
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
,我刪除記錄並保存上下文
- (void)refreshData
{
dispatch_queue_t fetchQ = dispatch_queue_create("Data Fetch", NULL);
dispatch_async(fetchQ, ^{
// here I delete some records
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self saveContext];
});
});
}
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
您是否在嘗試過self.tableview結束更新的情況下刪除該行。這可能會結束動畫。只是一個想法 –
聽起來好像你的begin和endUpdates可能不會被稱爲pairwise。您是否嘗試將每個呼叫記錄到語句旁邊,然後在日誌中查看它們是否以正確的順序和成對出現? – NSSplendid
@NSSplendid當我首先設置了一些斷點時,它碰到了controllerWillChangeContent。然後它擊中deleteRowsAtIndexPaths。最後,它會觸及controllerDidChangeContent。它看起來像正確的順序。 – robbert0