0

我已经在 Stack Overflow 和其他网站上梳理了几十个关于此的问题/答案,但我似乎无法让它发挥作用。这看起来是一个相当普遍的问题,但我发现的其他问题的答案都没有适用。基本上,我正在尝试将一个简单的 UITextView 添加到表格单元格中,但它没有显示出来。这是我所拥有的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    UITextView *textviewFactoid = nil;

    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:@"cell"];

        textviewFactoid = [[UITextView alloc]initWithFrame:CGRectMake(0, -3, 300, 25)];
        [textviewFactoid setFont:[UIFont systemFontOfSize:12.0f]];
        [textviewFactoid setBackgroundColor:[UIColor blueColor]];
        [textviewFactoid setTextColor:[UIColor grayColor]];
        [textviewFactoid setScrollEnabled:NO];
        [textviewFactoid setEditable:NO];
        [textviewFactoid setTag:98];
        [[cell contentView] addSubview:textviewFactoid];
    } else {
        textviewFactoid = (UITextView*)[cell.contentView viewWithTag:98];
    }

    NSString *textviewFactoidText = @"Here is the factoid";
    [textviewFactoid setText:textviewFactoidText];

    return cell;
}

有谁知道为什么单元格会是空白的?我将 backgroundColor 设置为蓝色,这样我就可以查看 textview 是否存在,但似乎并不存在。我也没有收到任何错误或警告来帮助我查明问题所在。

我已经从我编写的另一个应用程序中几乎逐字复制了这段代码,它在那里运行良好......我敢肯定这是我遗漏或更改的简单内容,但我有点难过。

有没有人比我眼尖?

4

1 回答 1

2

如果您正在为 iOS 6 构建并为您的单元格标识符注册了一个 nib 或类,dequeueReusableCellWithIdentifier则将始终返回一个单元格。您if (cell == nil) {永远不会评估为真并在该块内运行代码。

于 2013-06-20T20:56:19.810 回答