【发布时间】:2011-11-17 22:06:22
【问题描述】:
我有一个父类来处理所有 UITableview 持久数据管理和 UITableView 行管理。我已经从一个新的 XCode4 项目中复制了大部分代码,其中包含 UITableview 的持久数据。现在我正在尝试删除子类中的 tablerow,并且在删除行时不会调用任何委托方法,导致我的 tableview 处于不一致的状态。
我是否正确分配了代表?在这种情况下,谁是 NSFetechedResultsController 的代表? 我是否需要明确地将我的子类设为 NSFetchedResultsControllerDelegate?
这是我检索 NSFetchedResultsController 的方法,注意它是如何将 self 分配为委托的。
- (NSFetchedResultsController *)fetchedResultsController
{
if (__fetchedResultsController != nil)
{
return __fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:fetchBatchSize];
// Edit the sort key as appropriate.
// NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startMinute" ascending:YES];
NSArray *sortDescriptors;
if(sortDescriptor2!=nil)
{
sortDescriptors= [[NSArray alloc] initWithObjects:sortDescriptor,sortDescriptor2, nil];
}else
{
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
}
[fetchRequest setSortDescriptors:sortDescriptors];
if(filterPredicate != nil)
{
[fetchRequest setPredicate:filterPredicate];
}
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
// abort();
}
return __fetchedResultsController;
}
当需要删除表行时,不会调用任何委托方法,即使表行已被真正删除。为了调试问题,我添加了以下 2 个断言。第二个断言失败。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
id delegate = self.fetchedResultsController.delegate;
NSAssert(delegate!=nil,@"Tableview is not delegate of fetched results controller!");
//this assertion fails
NSAssert([((UITableViewController*)self) isEqual:delegate],@"Tableview is not delegate of fetched results controller!");
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the managed object for the given index path
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error = nil;
if (![context save:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
// abort();
}
}
}
谢谢!
【问题讨论】:
标签: objective-c uitableview ios5 iphone-4 nsfetchedresultscontroller