2011-10-23 76 views
0

我正在用CoreData更新UITableView。在启动时,第一行显示为(空)。如果我向下滚动/向上,它会加载。它只是不显示最初。UITableView显示(空)第一行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator]; 
    } 

    // set text for each cell 
    [cell.textLabel setText: [dataManager getProjectValueForKey: @"Title" atIndex: [indexPath row]]]; 
    [cell.detailTextLabel setText: [[[dataManager getProjectValueForKey: @"pid" atIndex:[indexPath row]] stringByAppendingString: @": "] stringByAppendingString: [dataManager getProjectValueForKey: @"Sponsor" atIndex:[indexPath row]]]]; 

    return cell; 
} 

dataManager是什么在跟Core Data交谈。我觉得它可能会落后或者在启动时发生什么,所以单元试图在数据准备好之前显示数据。但我不知道。

+0

如果您将第一个单元格关闭并返回到屏幕上,是否会加载正确的文本?此外,您可能需要查看NSFetchedResultsController以使用Core Data结果填充您的UITableView。其设计和高度优化的用例。 –

+0

是的,他们按照操作中提到的那样做。我有点新鲜。 –

+0

很难说出发生了什么事。我会在'-tableView:cellForRowAtIndexPath:'中设置一个断点。第一次,进入'-getProjectValueForKey:atIndex:',看看它为什么会回到零。 –

回答

1

实际上,Core Data中已经内置了一个很棒的“dataManager”。它被称为NSFetchedResultsController。您可以随时检索您的表格单元格中正确的数据对象与

[[self.fetchedResultsController fetchedObjects] objectAtIndex:indexPath.row]; 

这通常是内部tableView:cellForRowAtIndexPath:完成,而不是在一些其他的辅助功能。获取的结果控制器将负责检索所需的所有数据,以便显示它。

另请参阅Apple针对此通用设计模式的众多Core数据代码示例。