2013-04-08 81 views
1

我正在使用NSFetchedResultsController,我不知道如何解决我目前的问题。 我的表格视图中的标题是单元格而不是真正的标题,因为我不希望标题在滚动时粘在顶部。NSFetchedResultsController与自定义单元格

的信息是关于它的很清楚:

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 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 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) 

但中行的部分号码后1行的插入必须是2!我怎么让桌面视图知道这个?我已经在做这样的东西:

indexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 
           inSection:section]; 

newIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row + 1 
            inSection:section]; 

但在这是行不通的这种特殊情况下,先泻后一切正常,像它应该,因为这是唯一的一次有两个单元格,而只有一个从核心数据本身。

回答

1

我终于找到了解决我的问题!

我只是检查它是否是第一次通过NSFetchedResultsController委托(didChangeObject :)添加NSFetchedResultsController的单元格时,如果是,我手动添加另一行。

段:

- (void)controller:(NSFetchedResultsController *)controller 
    didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath 
    forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    NSInteger section = 1; 
    indexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 
            inSection:section]; 

    NSMutableArray *newIndexPaths = [NSMutableArray array]; 
    id <NSFetchedResultsSectionInfo> sectionInfo = [controller.sections objectAtIndex:0]; 
    if ([sectionInfo numberOfObjects] == 1) { 
     newIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row 
              inSection:section]; 
     [newIndexPaths addObject:newIndexPath]; 
    } 

    newIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row + 1 
             inSection:section]; 
    [newIndexPaths addObject:newIndexPath]; 

    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertRowsAtIndexPaths:newIndexPaths 
            withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
            withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     default: 
      break; 
    } 
}