1

我正在使用自定义表格视图来显示来自 Data Core 的数据,它应该看起来像这样

在此处输入图像描述

但我实际上得到的是这个

在此处输入图像描述

这是我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    Item *item = [self.items objectAtIndex:indexPath.row];
    UILabel *nameLabel = (UILabel *)[cell viewWithTag:100];
    nameLabel.text = item.name;
    UILabel *catLabel = (UILabel *)[cell viewWithTag:101];
    catLabel.text = item.category;
    UILabel *amountLabel = (UILabel *)[cell viewWithTag:102];
    amountLabel.text = item.amount;

}

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

    static NSString *CellIdentifier = @"AccountCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [self configureCell:cell atIndexPath:indexPath];

    return cell;

}
4

2 回答 2

1

您的其他数据源方法正在工作,因为您可以看到存在单元格,但没有填充任何内容。

这一行:

Item *item = [self.items objectAtIndex:indexPath.row];

当您使用获取的结果控制器时,这并不常见。它应该是

Item *item = [self.fetchedResultsController objectAtIndexPath:indexPath];

如果您签入调试器,您将看到该项目可能与您当前的代码为零。

如果不是这种情况,那么您的标签在原型单元格和您的代码之间不匹配。同样,这可以在调试器中检查 - 标签将为 nil。

于 2012-05-10T08:08:40.767 回答
1

如果您右键单击故事板中的标签,您可以看到它们连接到哪些插座。检查它们是否有一个白点来表示连接,然后按照@jhurton 的建议,检查您的数据项 is not nil

于 2012-05-10T08:19:11.290 回答