dueDate 属性を持つマネージド オブジェクトがあります。UITableView のセクション ヘッダーとして醜い日付文字列を使用して表示する代わりに、「category」という一時的な属性を作成し、次のように定義しました。
- (NSString*)category
{
[self willAccessValueForKey:@"category"];
NSString* categoryName;
if ([self isOverdue])
{
categoryName = @"Overdue";
}
else if ([self.finishedDate != nil])
{
categoryName = @"Done";
}
else
{
categoryName = @"In Progress";
}
[self didAccessValueForKey:@"category"];
return categoryName;
}
NSFetchedResultsController のセットアップは次のとおりです。
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task"
inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSMutableArray* descriptors = [[NSMutableArray alloc] init];
NSSortDescriptor *dueDateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dueDate"
ascending:YES];
[descriptors addObject:dueDateDescriptor];
[dueDateDescriptor release];
[fetchRequest setSortDescriptors:descriptors];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"category" cacheName:@"Root"];
テーブルは最初は正常に表示され、「進行中」というタイトルのセクションに dueDate が渡されていない未完成のアイテムが表示されます。これで、ユーザーはテーブル ビューの行をタップして、新しい詳細ビューをナビゲーション スタックにプッシュできます。この新しいビューで、ユーザーはボタンをタップして、アイテムが「完了」したことを示すことができます。ボタンのハンドラーは次のとおりです (self.task は管理対象オブジェクトです)。
- (void)taskDoneButtonTapped
{
self.task.finishedDate = [NSDate date];
}
「finishedDate」属性の値が変更されるとすぐに、次の例外が発生します。
2010-03-18 23:29:52.476 MyApp[1637:207] Serious application error. Exception was caught during Core Data change processing: no section named 'Done' found with userInfo (null)
2010-03-18 23:29:52.477 MyApp[1637:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no section named 'Done' found'
データセットで何かが変更されたことが NSFetchedResultsController に通知されたため、新しい詳細ビューによって現在非表示になっている UITableView が行とセクションを更新しようとしていることがわかりました。これが私のテーブル更新コードです (Core Data Recipes サンプルまたは CoreBooks サンプルのいずれかからコピーしたもので、どちらを思い出せません)。
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[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];
}
これらの各関数にブレークポイントを設定したところ、controllerWillChange のみが呼び出されることがわかりました。controller:didChangeObject:atIndexPath:forChangeType:newIndex または controller:didChangeSection:atIndex:forChangeType が呼び出される前に、例外がスローされます。
この時点で私は立ち往生しています。sectionNameKeyPath を単に「dueDate」に変更すると、すべて正常に動作します。これは、dueDate 属性が変更されないのに対し、finishedDate 属性が変更された後に読み返すとカテゴリが異なるためだと思います。
助けてください!
アップデート:
ここに私の UITableViewDataSource コードがあります:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo name];
}