我有一个带有 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"];
该表最初显示良好,在标题为“进行中”的部分中显示了到期日期尚未通过的未完成项目。现在,用户可以点击表格视图中的一行,将新的详细信息视图推送到导航堆栈上。在这个新视图中,用户可以点击一个按钮来指示该项目现在“完成”。这是按钮的处理程序(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'
我已经设法弄清楚当前被新的详细信息视图隐藏的 UITableView 正在尝试更新其行和部分,因为 NSFetchedResultsController 被通知数据集中发生了某些更改。这是我的表格更新代码(从 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];
}