我很疑惑为什么reloadData没有重新载入tableview。它不叫numberOfRowsInSection
...UITableView reloadData不重新载入
的fetchedResultsController
救了新的数据到核心数据
之前添加新的数据的实现代码如下
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)不是nilData
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];
...
发现了另一个问题。在保存数据并在尝试滚动时返回tableview时,它只显示白色单元格。它也不会重新加载旧单元格。
你尝试调用'reloadData'在'viewDidAppear'的信息? – dasdom 2011-12-29 18:24:47
是的,我做了...没有变化 – Chris 2011-12-29 18:30:52