【问题标题】:UITableViewCell detailTextLabel never displaysUITableViewCell detailTextLabel 从不显示
【发布时间】:2015-09-15 21:46:37
【问题描述】:

UITableViewCell detailTextLabel 从不显示。

没有 xib,没有故事板。 我试过 UITableViewCellStyleValue1、UITableViewCellStyleValue2 和 UITableViewCellStyleSubtitle。我没有使用子类 UITableViewCells。我已经尝试过 detailTextLabel.text 和 detailTextLabel.attributedText。我已经验证了重用 ID 没有在其他任何地方使用,并且我已经通过并确认它是正确的值。 textlabel 有效,如果我设置了 accessoryType 也可以。只是detailTextLabel拒绝显示。

值得注意的是,(cell == nil) 条件从不命中,我不知道为什么。我尝试将重用 ID 设置为乱码,以查看它是否会产生任何效果但没有成功。我错过了什么?

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class]
           forCellReuseIdentifier:@"reuseCellID"];
    [self setTitle:@"Title"];
}


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

    static NSString *reuseCellID = @"reuseCellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCellID
                                                            forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
                                     reuseIdentifier:reuseCellID];
        cell.detailTextLabel.text = @"detail (one)";
    }

    cell.textLabel.text = self.collection[indexPath.row];
    cell.detailTextLabel.text = @"detail (two)";
    /*NSMutableAttributedString *textLabelStr =
        [[NSMutableAttributedString alloc] initWithString:@"attributed detail"];
    cell.detailTextLabel.attributedText = textLabelStr;*/

    return cell;
}

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    if (cell == nil) 永远不会被命中,因为您使用了 dequeueReusableCellWithIdentifier:forIndexPath: 和 registerClass: forCellReuseIdentifier:。

    摆脱对registerClass: forCellReuseIdentifier:的调用并使用dequeueReusableCellWithIdentifier:(没有forIndexPath:。

    然后你的代码会做你期望的。

    顺便说一句 - 不要在 if (cell == nil) 中设置 detailTextLabel.text,除非您希望每个单元格都具有相同的详细文本。

    【讨论】:

    • 我在单元格中设置 detailTextLabel == nil 条件,同时调试。我一定不会把它放在那里。但是,如果我退出对 registerClass:forCellReuseIdentifier: 的调用,那么我似乎记得应用程序崩溃并告诉我需要注册单元格(但这可能是我之前使用 xib 时的实验)。
    • 就是这样。删除该调用以及 forIndexPath: 会处理它。我以为你必须调用 registerClass:forCellReuseIdentifier: 如果你使用 dequeueReusableCellWithIdentifier: ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2012-03-17
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    相关资源
    最近更新 更多