我目前正在開發像應用程序這樣的假日期刊,它按照類型存儲您訪問過的每個地方。例如,餐館名稱應該存儲在「食物」部分下。我已經設法使用核心數據並創建了沒有問題的表。然而,問題是,每當我試圖改變一個地方(從而實現代碼如下需要重新排序)的類型,我會得到一個錯誤信息:爲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];
}
然後抓取結果控制器的委託,我會在收到錯誤信息.......
請發佈您所做的代碼調用 – TahoeWolverine 2009-07-22 22:52:26