【问题标题】:UITableViewCell not showing subview UITextViewUITableViewCell 不显示子视图 UITextView
【发布时间】:2013-06-20 20:46:59
【问题描述】:

我已经在 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 是否存在,但似乎并不存在。我也没有收到任何错误或警告来帮助我查明问题所在。

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

有谁比我的眼睛更敏锐?

【问题讨论】:

    标签: ios uitableview uitextview


    【解决方案1】:

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

    【讨论】:

    • 是的,在你的 if 块中设置一个断点,因为这个答案中所述的原因,它没有被调用。
    • 是的,最好在 Interface Builder 中使用自定义 UITableViewCell 类来完成。
    • 很公平。现在我开始学习如何制作自定义 Cell 类。谢谢各位!
    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多