2011-12-29 123 views
3

我很疑惑为什么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时,它只显示白色单元格。它也不会重新加载旧单元格。

enter image description here

+0

你尝试调用'reloadData'在'viewDidAppear'的信息? – dasdom 2011-12-29 18:24:47

+0

是的,我做了...没有变化 – Chris 2011-12-29 18:30:52

回答

5

好的!我发现了这个问题。我使用了beginUpdates和endUpdates来代替reloadData。非常感谢您的帮助!我从NSFetchedResultsControllerDelegate Reference

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

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

工作就像一个魅力.... – Siten 2015-01-07 09:47:53

1
在我看来

您尝试进行读取时,控制器是零。我会尝试这种方式:

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

fetschedResultsController不是零,因为它得到延迟加载 – Chris 2011-12-29 18:30:41

1

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

尝试添加以下内容:

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

是否能解决这个问题,你可以动画表的修改VS重新加载整个表。 NSFetchedResultsController文档有一个清楚的例子,说明如何做到这一点。

+0

controllerDidChangeContent被调用,但它不会对表进行任何更改。 – Chris 2011-12-29 19:43:20

+0

你需要发布更多的代码。你所问题的唯一线索是问题是在数据被保存并且'numberOfSectionsInTableView'没有被调用。这通常意味着要么不在适当的时间重新加载表,要么在tableView dataSource方法中存在错误。 – XJones 2011-12-29 20:06:05

+0

数据已保存在其他控制器中。您需要查看哪些代码? – Chris 2011-12-29 22:08:17

0

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

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

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

+0

我已将我的cellForRowAtIndexPath添加到上一个问题。数据是从FRC直接提取的。但是在执行reloadData之后它不会被调用。 – Chris 2011-12-30 07:31:46