UITableView reloadData不重新加载

我很疑惑为什么reloadData不重新加载tableview。 它不会调用numberOfRowsInSection

在将新数据保存到核心数据之后, fetchedResultsController确实会得到新的一行

在将新数据添加到tableview之前

 2011-12-29 19:09:08:213 CaveConditions[56423:71939] FRC 170 

viewWillAppear并添加数据

 2011-12-29 19:09:35:908 CaveConditions[56423:71939] FRC NEW 171 

在调用reloadData之前,tableView(aTableView)不是零

 2011-12-29 19:18:27:334 CaveConditions[56525:71939] TableVIew: <UITableView: 0x9c12000; frame = (0 0; 320 367); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x751cbb0>; contentOffset: {0, 0}> 

一旦我重新启动应用程序,它显示新的内容…我正在使用故事板,并已完成tableview和委托/数据源之间的所有连接,并多次检查它。 该类是一个UIViewController。

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.fetchedResultsController = nil; NSError *error; if (![[self fetchedResultsController] performFetch:&error]) { // Update to handle the error appropriately. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:0]; NSLog(@"FRC NEW %d",[sectionInfo numberOfObjects]); [self.aTableView reloadData]; } 

不知道这里有什么问题…有什么想法?

编辑:

我正在懒加载FRC

 - (NSFetchedResultsController *)fetchedResultsController { if (!fetchedResultsController) { NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; if (isNormalCell) { fetchRequest.entity = [NSEntityDescription entityForName:@"Condition" inManagedObjectContext:self.context]; fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"diveDate" ascending:NO]]; fetchRequest.predicate = [NSPredicate predicateWithFormat:@"cave = %@", cave]; } else { fetchRequest.entity = [NSEntityDescription entityForName:@"Condition" inManagedObjectContext:self.context]; fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"insertDate" ascending:NO]]; fetchRequest.returnsDistinctResults = YES; } fetchRequest.fetchBatchSize = 20; fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil]; fetchedResultsController.delegate = self; } return fetchedResultsController; } 

这里有一些代码

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier; if (!isNormalCell) CellIdentifier = @"conditionCellReport"; else CellIdentifier = @"conditionCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // Set up the cell... [self configureCell:cell atIndexPath:indexPath]; return cell; } - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { Condition *condition = [fetchedResultsController objectAtIndexPath:indexPath]; ConditionCell *conCell = (ConditionCell *)cell; // Reset cell tag which normally contains the ConditionID conCell.tag = 0; conCell.img.image = nil; conCell.lineLabel.text = [condition.line valueForKey:@"line"]; conCell.flowProgress.progress = [[condition.flow valueForKey:@"flowValue"] floatValue]; conCell.percolationProgress.progress = [[condition.percolation valueForKey:@"percolationValue"] floatValue]; conCell.sedimentProgress.progress = [[condition.sediment valueForKey:@"sedimentValue"] floatValue]; conCell.visProgress.progress = [[condition.visibility valueForKey:@"visValue"] floatValue] / 25; conCell.tag = [condition.ccId intValue]; ... 

发现另一个问题。 保存数据并回滚到桌面视图时,它只显示白色单元格..它不会重新加载旧的单元格。

在这里输入图像说明

好! 我发现了这个问题。 我用beginUpdates和endUpdates来代替reloadData。 非常感谢您的帮助! 我从NSFetchedResultsControllerDelegate参考中获得了信息

 - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } 

在我看来,当控制器为零时,您尝试执行一次获取。 我会试试这个方法:

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.fetchedResultsController = nil; id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:0]; NSLog(@"FRC NEW %d",[sectionInfo numberOfObjects]); NSError *error; if (![[self fetchedResultsController] performFetch:&error]) { // Update to handle the error appropriately. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } [self.aTableView reloadData]; 

如果表格显示正确的数据,当你重新启动应用程序,那么你的tableView的dataSource和委托方法是正确的。 您提到添加新数据(即保存managedObjectContext )后会发生问题。 如果是这样的话,那么问题在于你处理NSFetchedResultsController委托方法。

尝试添加以下内容:

 - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.aTableView reloadData]; } 

如果解决了这个问题,你可以animation表更改与重新加载整个表。 NSFetchedResultsController文档有一个清楚的例子,说明如何做到这一点。

如果添加的数据显示应用程序重新启动后,我会假定您的代码是缺less将新数据添加到提供给以下方法的数组的步骤。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

为了澄清,具有171个对象的FRC应该将这些信息传递给将用于重新加载UITableView的数组。