首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell detailTextLabel从不显示

UITableViewCell detailTextLabel从不显示
EN

Stack Overflow用户
提问于 2015-09-15 21:46:37
回答 1查看 214关注 0票数 0

UITableViewCell detailTextLabel从不显示。

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

值得注意的是,(单元格==为零)条件从未被击中,我不知道为什么。我试着把重复使用的ID设置为胡言乱语,只是想看看它是否有任何效果,但没有成功。我错过了什么?

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-15 22:08:38

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

摆脱对registerClass: forCellReuseIdentifier:的调用,使用dequeueReusableCellWithIdentifier: (不使用forIndexPath: )。

然后,您的代码将执行您的期望。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32596215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档