简体   繁体   English

UITableView reloadData不会重新加载

[英]UITableView reloadData does not reload

I am quite puzzled why reloadData does not reload the tableview. 我很困惑为什么reloadData不会重新加载tableview。 It does not call numberOfRowsInSection ... 它不会调用numberOfRowsInSection ...

The fetchedResultsController does get the new row after having saved the new data into core-data fetchedResultsController在将新数据保存到核心数据后确实获得了新行

Before adding new data to the tableview 在将新数据添加到tableview之前

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

After viewWillAppear and adding data viewWillAppear之后并添加数据

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

The tableView (aTableView) is not nil before calling reloadData 在调用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}>

As soon as I restart the app it shows the new content... I am using Storyboard and have done all the connection between the tableview and the delegate / data source and checked it multiple times. 一旦我重新启动应用程序,它就会显示新内容...我正在使用Storyboard并完成了tableview和委托/数据源之间的所有连接并多次检查它。 The Class is a UIViewController. 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];
}

Not sure what the problem is here... Any ideas? 不知道这里有什么问题......有什么想法吗?

Edit: 编辑:

I am lazyloading FRC 我懒得加载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;
}

Here some code 这里有一些代码

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

Found another problem. 发现另一个问题。 After saving the data and going back to the tableview when trying to scroll it shows only white cells.. it does not reload the old cells too. 保存数据并在尝试滚动时返回到tableview它只显示白色单元格..它也不会重新加载旧单元格。

在此输入图像描述

OK! 好! I found the problem. 我发现了这个问题。 Instead of reloadData I used beginUpdates and endUpdates. 我使用的是beginUpdates和endUpdates,而不是reloadData。 Thanks a lot for all your help! 非常感谢您的帮助! I got the information from the NSFetchedResultsControllerDelegate Reference 我从NSFetchedResultsControllerDelegate Reference获得了信息

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

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

in my opinion you try to perform a fetch when the controller is nil. 在我看来,你试图在控制器为零时执行提取。 i would try it this way: 我会这样试试:

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

If the table is showing the correct data when you restart the app then your tableView dataSource and delegate methods are correct. 如果表在重新启动应用程序时显示正确的数据,那么tableView dataSource和委托方法是正确的。 You mention that the problem occurs after you add new data (ie save the managedObjectContext ). 您提到在添加新数据后发生问题(即保存managedObjectContext )。 If that's the case, then the issue is in your handling of the NSFetchedResultsController delegate methods. 如果是这种情况,那么问题在于您处理NSFetchedResultsController委托方法。

Try adding the following: 尝试添加以下内容:

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

If that fixes the issue, you can animate the table changes vs reloading the whole table. 如果这样可以解决问题,则可以为表更改设置动画并重新加载整个表。 The NSFetchedResultsController docs have a clear example of how to do this. NSFetchedResultsController文档有一个如何执行此操作的明确示例。

If the added data displays after the app restarts, I would assume that your code is missing the step to add the new data to the array that is supplied to the following method. 如果在应用程序重新启动后显示添加的数据,我会假设您的代码缺少将新数据添加到提供给以下方法的数组的步骤。

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

To clarify, the FRC with 171 objects should pass that information on to the array that will be used to reload the UITableView. 为了澄清,具有171个对象的FRC应该将该信息传递给将用于重新加载UITableView的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM