我开始工作(学习)处理 UITableView
核心数据,我开始阅读有关它的内容,在我开始编写一个小样本应用程序之后,我可以按下按钮,然后数据将显示在 tableView
中 .
我的问题是我已经正确编程了,我认为,Xcode没有向我显示任何警告或错误等 . 但它不起作用 .
任何人都可以告诉我为什么这段代码不起作用?
项目:http://www.sendspace.com/file/ohc1kn
编辑:如果你没有设置: self.fetchedResultsController.delegate = nil;
显示以下错误:
2013-02-17 16:45:05.480 tableView Coredata [1533:207] *断言失败 - [UITableView _createPreparedCellForGlobalRow:withIndexPath:],/ SourceCache / UIKit_Sim / UIKit-1912.3 /UITableView.m:6072 2013-02-17 16:45:05.481 tableView Coredata [1533:207] CoreData:错误:严重的应用程序错误 . 在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常 . UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:with userInfo(null)
代码用于将NewObject插入到Database和TableView中,如果执行该操作,则会出现上述错误 .
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
// Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
// 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.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView: cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
2 回答
1错误是缺少tableView参数:
- (UITableViewCell *)tableView: cellForRowAtIndexPath:(NSIndexPath *)indexPath
解决这个问题然后告诉我们究竟什么不起作用/你希望你的应用程序做什么
你应该在这里提供其他细节 . 理解正在发生的事情并不容易,但与此同时,我没有多少考虑因素 .
首先,你使用的是
registerClass:forCellReuseIdentifier:
方法吗?如果不是,你应该在tableView:cellForRowAtIndexPath:
方法中使用alloc-init
单元格 .然后,以正确的方式覆盖
tableView:cellForRowAtIndexPath:
,如下所示