我一直在努力掌握CoreData。在几次尝试找到一个好的教程后,我在youtube上的教程取得了很大的进展。然而,当我到达NSFetchedResultsController
时,我在自定义表单元格ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath]
上收到一条警告,指出它是一个用UITableViewCell
初始化的不兼容指针(本教程不使用自定义单元格)。
更糟糕的是,当我试图添加一个项目时,我会遇到这个漂亮而丑陋的错误。
2013-12-04 19:15:48.407 Ping〔490:60b〕*断言失败-[UTableView_endCellAnimationsWithContext:],/SourceCache/UIKit/UIKit-2903.23/UITableView.m:1138 2013-12-0419:15:48.409 Ping[490:60b]核心数据:错误:严重的应用程序错误。从的委托中捕获到异常调用期间的NSFetchedResultsController-controllerIDChangeContent:。尝试将行0插入到节0中,但在使用userInfo更新后,节0中只有0行(空)
我花了几个小时试图弄清楚我做错了什么,但我无法理解。所以,如果有人能看看我的代码,并就如何修复它给我建议,我将不胜感激。谢谢。
控制器:
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.ContactsListTableView;
switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate: {
Contact *changeContact = [self.fetchedResultsController objectAtIndexPath:indexPath];
ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.name.text = [NSString stringWithFormat:@"%@ %@", changeContact.contactFirstName, changeContact.contactLastName];
}
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
编辑:我实现的行数和节数。
- (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];
}
fetchedResultsController
在接口中被简单地定义为@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController
警告是指方法
[tableView cellForRowAtIndexPath:indexPath];
返回一个UITableViewCell,它实际上不是您的ITDContactCell。在方法之前放置强制转换应删除警告。
ITDContactCell *cell = (ITDContactCell*) [tableView cellForRowAtIndexPath:indexPath];
另一个错误可能是由于fetchedResultsController和tableView之间缺乏关系。显示tableView委托和源方法会有所帮助。我想你已经实现了类似的东西:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSError *error = nil;
if(![self.fetchedResultsController performFetch:&error]) {
// NSLog(@"Error! %@", error);
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _fetchedResultsController.sections.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [secInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
ITDContactCell *cell = (ITDContactCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// assign cell content
return cell;
}
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}
-(NSFetchedResultsController *) fetchedResultsController {
if(_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.ContactsListTableView;
switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate: {
Contact *changeContact = [self.fetchedResultsController objectAtIndexPath:indexPath];
ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.name.text = [NSString stringWithFormat:@"%@ %@", changeContact.contactFirstName, changeContact.contactLastName];
}
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
编辑1:
请注意,您不应该在表视图中使用reloadData方法,因为表是在保存对NSManagedObjectContext的更改后重新加载的(通过提取的结果控制器)。
编辑2:
您是NSManagedObjectContext nil吗?