2011-05-11 54 views
1

我的表用于列出核心数据中的行。一切工作正常,我已经使用两种类型的数据加载在同一个表视图。数据类型因范围按钮更改而改变。两个范围按钮之一显示已完成的任务,而其他显示未完成的任务我可以在桌面上搜索,更新。虽然只完成DATAS的列表是可编辑的,可以被删除,有时它工作正常,有时它给误差等,应用程序在删除行时崩溃

Serious application error. An exception was caught from the delegate of 
NSFetchedResultsController during a call to -controllerDidChangeContent:. *** - 
[NSMutableArray insertObject:atIndex:]: index 4 beyond bounds for empty array with 
userInfo (null) 

的获取结果控制器初始化为,

- (NSFetchedResultsController *)fetchedResultsControllerWithPredicate:(NSPredicate *)predicate{ 

if (self.fetchedResultsControl != nil) { 
    NSLog(@"Here I am outside"); 

    return self.fetchedResultsControl; 

} 

的代码加载小区的tableview是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

CustomCell *cell =(CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 
    for (id currentObject in topLevelObjects) { 
     if([currentObject isKindOfClass:[UITableViewCell class]]){ 
      cell=(CustomCell *)currentObject; 
      break; 
     } 
    } 

} 

[self configureCell:cell atIndexPath:indexPath]; 
return cell; 

}

甲nd NSFetchedResultsControllerDelegate调用的更新方法为;

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { 
[self.listingTable beginUpdates]; 
NSLog(@"Changed content"); 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id < NSFetchedResultsSectionInfo>)sectionInfo 
     atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type { 

switch(type) { 
    case NSFetchedResultsChangeInsert: 
     [self.listingTable insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [self.listingTable deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
} 

}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
    newIndexPath:(NSIndexPath *)newIndexPath { 
NSLog(@"Changed content switch case"); 
UITableView *tableView = self.listingTable; 

switch(type) { 

    case NSFetchedResultsChangeInsert: 
     [self.listingTable insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
         withRowAnimation:UITableViewRowAnimationTop]; 
     break; 

    case NSFetchedResultsChangeDelete: 
     [self.listingTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 

    case NSFetchedResultsChangeUpdate: 
     [self configureCell:(CustomCell *)[tableView cellForRowAtIndexPath:indexPath] 
       atIndexPath:indexPath]; 

     break; 

    case NSFetchedResultsChangeMove: 
     [self.listingTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     [self.listingTable insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
         withRowAnimation:UITableViewRowAnimationFade]; 
     break; 
} 
} 


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { 
NSLog(@"Changed Content"); 
[self.listingTable endUpdates]; 

} 

回答

3

我没有看到任何明显的错误,但在一个tableview中换出两个逻辑表是一个危险的设计,因为你必须要非常小心,tableview中知道其逻辑表已经变了。

你得到的错误提示tableview使用一个逻辑表的索引来配置自己,但实际上是访问第二个逻辑表的数组更短。

您的直接解决方案是仔细查看numberOfSectionsnumberOfRowsInSection等方法,以确保在切换逻辑表时能够正确更新。

最好的设计解决方案是使用两个独立的tableviews和自己单独的数据源,然后根据需要将它们交换出来。

+0

我在numberOfRowsInSection中实现了日志,但更新是正确的,因此删除前的行数大于1,然后删除。 – Sandeep 2011-05-12 08:15:10

+0

但也尝试打印托管对象的未见行,所以日志给出的消息:(实体:Memos; id:0x7150c10 ;数据:)。但是一旦表格滚动,删除就成为可能。这是使用两个单独的列表与相同的fetchedresultscontroller和相同的表视图的错误。 – Sandeep 2011-05-12 08:20:58

+0

我不能肯定你的设计是造成问题,但我认为它可能。 – TechZen 2011-05-12 22:28:51