ios - UITableView reloadData 不重新加载

标签 ios uitableview

我很纳闷为什么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 和委托(delegate)/数据源之间的所有连接并多次检查。该类是一个 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 后,它只显示白色单元格。它也不会重新加载旧单元格。

enter image description here

最佳答案

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

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

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

关于ios - UITableView reloadData 不重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8671621/

相关文章:

ios - 绘制路径描边和阴影(iOS)

ios - Storyboard 中的内部不一致

swift - 我无法使用 UIButton 从 UIViewController 到 UITableViewController

ios - 从 UITableView 中删除行时出现 NSInternalInconsistencyException

iphone - 实现自定义 UITableViewCellAccessoryType 以用作复选标记(如待办事项列表)的最佳方法

ios - 激活元素时如何获取indexpath.row?

ios - 无效的浮点值 - swift 3 ios

ios - UITableView 中的自定义单元格(iOS 9)

iphone - UIActivityController 是如何呈现的?

ios - UITableView 在滚动表格时使用 NSTimer 重新加载数据