UITableView reloadData不会重新加载

Chr*_*ris 3 uitableview ios

我很困惑为什么reloadData不会重新加载tableview.它没有打电话numberOfRowsInSection......

fetchedResultsController将新数据保存到核心数据之后,确实获得了新行

在将新数据添加到tableview之前

2011-12-29 19:09:08:213 CaveConditions[56423:71939] FRC 170
Run Code Online (Sandbox Code Playgroud)

之后viewWillAppear并添加数据

2011-12-29 19:09:35:908 CaveConditions[56423:71939] FRC NEW 171
Run Code Online (Sandbox Code Playgroud)

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

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}>
Run Code Online (Sandbox Code Playgroud)

一旦我重新启动应用程序,它就会显示新内容...我正在使用Storyboard并完成了tableview和委托/数据源之间的所有连接并多次检查它.Class是一个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];
}
Run Code Online (Sandbox Code Playgroud)

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

编辑:

我懒得加载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;
}
Run Code Online (Sandbox Code Playgroud)

这里有一些代码

- (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];
...
Run Code Online (Sandbox Code Playgroud)

发现另一个问题.保存数据并在尝试滚动时返回到tableview它只显示白色单元格..它也不会重新加载旧单元格.

在此输入图像描述

Chr*_*ris 5

好!我发现了这个问题.我使用的是beginUpdates和endUpdates,而不是reloadData.非常感谢您的帮助!我从NSFetchedResultsControllerDelegate Reference获得了信息

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

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)