2013-02-09 63 views
0

我有一个应用程序使用连接到UITableViewNSFetchedResultsControllerNSFetchedResultsController覆盖插入的最后一个单元格

我遇到了一个非常奇怪的问题,因为当一个新的项目应该出现在表格底部的插入完成后,最后一个项目的单元格会被新插入的项目替换 - 屏幕现在显示我刚刚插入的项目,但在此之前的项目已丢失。进一步插入显示相同的行为,表不增长,最后一项被替换为任何新项目。

重新启动应用程序时,应该显示完整的数据表(换句话说,覆盖的单元格在数据库中仍有数据)。看起来结果控制器失去了新项目应该去的地方。

的之前和之后的一个例子:

Before

After

ADDITION:

现有代码controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 
    UITableView *fetchTableView = [self tableViewForFetchedResultsController:controller]; 
    switch(type) { 
     case NSFetchedResultsChangeInsert: 
      [fetchTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
       break; 
///.... 
    } 
+0

这取决于您如何更新表,大概在fetchd结果控制器委托回调中。请显示你的代码。 – Mundi 2013-02-09 11:04:11

回答

0

这种现象似乎吨o表示在违规的更新代码中,您正在更新最后一个单元格,而不是插入一个。

使用
insertRowsAtIndexPaths:withRowAnimation:而不是
reloadRowsAtIndexPaths:withRowAnimation:

0

我猜你使用了错误的参数在你的方法:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 

小心使用newIndexPath参数,不indexPath,插入时一排:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { 
    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     default: 
      break; 
    } 
} 
相关问题