【问题标题】:UITableView reloadData does not reloadUITableView reloadData 不会重新加载
【发布时间】:2011-12-29 18:15:05
【问题描述】:

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

fetchedResultsController 在将新数据保存到 core-data 后确实获得了新行

在向 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)不为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}>

一旦我重新启动应用程序,它就会显示新内容...我正在使用 Storyboard,并且已经完成了 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 后,它只显示白色单元格.. 它也不会重新加载旧单元格。

【问题讨论】:

  • 您是否尝试在viewDidAppear 中致电reloadData

标签: ios uitableview


【解决方案1】:

好的!我发现了问题。我没有使用 reloadData,而是使用了 beginUpdates 和 endUpdates。非常感谢您的帮助!我从NSFetchedResultsControllerDelegate Reference得到信息

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

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

【讨论】:

    【解决方案2】:

    在我看来,当控制器为 nil 时,您会尝试执行 fetch。我会这样尝试:

    - (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];
    

    【讨论】:

    • fetschedResultsController 不为零,因为它被延迟加载
    【解决方案3】:

    如果在您重新启动应用程序时表格显示正确的数据,那么您的 tableView 数据源和委托方法是正确的。您提到添加新数据后会出现问题(即保存managedObjectContext)。如果是这种情况,那么问题在于您对 NSFetchedResultsController 委托方法的处理。

    尝试添加以下内容:

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

    如果这样可以解决问题,您可以为表格更改设置动画,而不是重新加载整个表格。 NSFetchedResultsController 文档有一个明确的示例说明如何执行此操作。

    【讨论】:

    • controllerDidChangeContent 被调用,但它不会对表进行任何更改。
    • 您需要发布更多代码。您所问问题的唯一线索是问题出在保存数据之后并且没有调用numberOfSectionsInTableView。这通常意味着要么您没有在正确的时间重新加载表格,要么您的 tableView 数据源方法中存在错误。
    • 数据被保存在另一个控制器中..你需要看什么代码?
    • 您是否将数据保存到不同于fetchedResultsController 中配置的managedObjectContext
    • 不,它与我从 App 委托获得的 managedObjectContext 相同。否则我猜FRC在保存后不会有171个条目?我已将一些代码添加到我的问题中
    【解决方案4】:

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

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

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

    【讨论】:

    • 我已将 cellForRowAtIndexPath 添加到上一个问题中。数据直接从 FRC 中提取。但是在我执行 reloadData 后它不会被调用。
    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多