2011-10-24 12 views
3

私はUITableViewに2 NSFetchedResultsControllersを使用しています。各NSFetchedResultsControllerにはセクションが1つしかありません。ただし、表には4つのセクションがあります。私はNSFetchedResultsControllersのいずれかの結果をテーブルの第4セクションに設定します。これまではすべてうまく動作します。しかし、ユーザーが最初のセクションの最初のセルを削除すると、NSFetchedResultsControllersが変更されます。表の最後のセクションの行が削除されることがあります。このメソッドが呼び出されるとき:複数のNSFetchedResultsController - didChangeObject

- (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:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     NSLog(@"section: %d, row: %d", [newIndexPath section], [newIndexPath row]); 

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
... 
} 

セクションは、NSFetchedResultsControllersのセクションであるため、常に0です。したがって、セクションは、テーブルビューの正しいものと一致しません。

回避策はありますか?私は基本的にNSFetchedResultsControllerのセクションを0ではなく3に変更したいと思っています。

答えて

2

私は回避策を見つけましたが、よりきれいな解決策があることがうれしいです。

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
    newIndexPath:(NSIndexPath *)newIndexPath 
{ 
UITableView *tableView = self.tableView; 
if (newIndexPath != nil && controller == self.fetchedXController) { 
    newIndexPath = [NSIndexPath indexPathForRow:[newIndexPath row] inSection:3]; 
    if ([tableView cellForRowAtIndexPath:newIndexPath] == nil) { 
     type = NSFetchedResultsChangeInsert; 
    } 
} 
if (indexPath != nil && controller == self.fetchedDomainsController) { 
    indexPath = [NSIndexPath indexPathForRow:[indexPath row] inSection:3]; 
} 

switch(type) { 
    case NSFetchedResultsChangeInsert: 
     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeUpdate: 
     [self configureCell:[tableView cellForRowAtIndexPath:newIndexPath] atIndexPath:newIndexPath]; 
     break; 
... 
関連する問題