2009-07-22 69 views
1

我目前正在开发像应用程序这样的假日期刊,它按照类型存储您访问过的每个地方。例如,餐馆名称应该存储在“食物”部分下。我已经设法使用核心数据并创建了没有问题的表。然而,问题是,每当我试图改变一个地方(从而实现代码如下需要重新排序)的类型,我会得到一个错误信息:为UITableView重新排序提取结果控制器

2009-07-22 21:04:58.150 HolidayTest[8662:20b] Serious application error. Exception was caught during Core Data change processing: *** -[NSCFArray removeObjectAtIndex:]: index (4) beyond bounds (4) with userInfo (null) 
2009-07-22 21:04:58.151 HolidayTest[8662:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray removeObjectAtIndex:]: index (4) beyond bounds (4)' 

我知道读取结果控制器的委托已更新的表。但是问题是fetch控制器没有更新自己的段和行数据。简单的方法是告诉读取结果控制器在用户更改其类型时重新获取其数据。但是,这不是管理数据的有效方法。


谢谢你的回答TahoeWolverine。这里是我改变用户在tableview中选择一行以改变其类型的地方的类型的代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:selected inSection:0]; 
    UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath]; 
    checkedCell.accessoryType = UITableViewCellAccessoryNone; 

    [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark]; 
    selected = indexPath.row; 
    NSManagedObject *aType = [siteType objectAtIndex:indexPath.row]; 
    self.site.type = [aType valueForKey:@"name"]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [self.delegate TypeSelectionController:self didChangeType:YES]; 
} 

这段代码对应于运行下面的代码

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView beginUpdates]; 
} 


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

     case NSFetchedResultsChangeUpdate: 
      [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; 
      break; 

     case NSFetchedResultsChangeMove: 
      [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
    } 

} 

- (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)controllerDidChangeContent:(NSFetchedResultsController *)controller 
{ 
    [self.tableView endUpdates]; 
} 

然后抓取结果控制器的委托,我会在收到错误信息.......

+0

请发布您所做的代码调用 – TahoeWolverine 2009-07-22 22:52:26

回答

0

这是很难准确地告诉你在这里意味着什么,但是好像你试图在阵列的末尾移除。很难知道你会做什么导致这一点。

如果我要实现这一点,我会

a)从表中的条目:

[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 

B)取下数据结构中的条目。

C)修改数据(标题和其他)。 D)在数据结构中添加它需要的条目。

E)添加条目,其中应该在表:

[tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade]; 

希望这有助于。如果你更新了这个问题,我会保持注意。