1

在我的 UITableView 中,我最近将单元格的结构从以前只是将 UILabels 放在单元格的 contentView 中,改为在 contentView 中添加两个 UIView(CellFront 和 CellBack,彼此重叠)(这样我可以实现滑动效果通过滑动顶部并显示较低的)并将 UILabels 添加到顶部 UIView。

但是,现在,无论出于何种原因,单元格永远不会被初始化,因此我的 UITableView 充满了空白单元格。

我的单元格创建如下(ArticleCell 是 UITableViewCell 的子类):

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

    ArticleInfo *articleInfo = [self.fetchedResultsController objectAtIndexPath:indexPath];

    // Checks if user simply added a body of text (not from a source or URL)
    if ([articleInfo.isUserAddedText isEqualToNumber:@(YES)]) {
        CellIdentifier = @"BasicArticleCell";
    }
    else {
        CellIdentifier = @"FullArticleCell";
    }

    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // If the user simply added a body of text, only articlePreview and progress has to be set
    cell.preview = articleInfo.preview;

    // If it's from a URL or a source, set title and URL as well
    if ([articleInfo.isUserAddedText isEqualToNumber:@(NO)]) {
        cell.title = articleInfo.title;
        cell.URL = articleInfo.url;
    }

    return cell;
}

但是我在 if 语句中的上面的 initWithStyle 方法上设置了一个断点,它永远不会被调用:

在此处输入图像描述

什么会导致这个?我每次都在删除应用程序并从头开始构建它,因此数据肯定会添加到 UITableView 中,但所有单元格都是空白的。我可以告诉我正在添加一堆单元格,因为我在所有这些单元格上都有披露指示符,而表格视图只是充满了只有指示符的空单元格。

在此处输入图像描述

我究竟做错了什么?

4

1 回答 1

2

尝试

ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

代替

ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

第一个是旧的标准方式。它不会为您创建一个单元格。而第二个单元将从情节提要中创建。因此,如果您使用情节提要,您确实应该使用您现在使用的方法,但它永远不会向 if 分支提供信息,因为单元格永远不会为零。


当实例化表单故事板时,initWithStyle:reuseIdentifier:永远不会被调用。要么设置好一切,-initWithCoder:要么-layoutSubviews

于 2013-04-14T17:26:57.573 回答