繁体   English   中英

iOS UITableViewCell detailTextLabel不显示

[英]iOS UITableViewCell detailTextLabel not showing

我希望每个单元都有detailTextLabel显示。

用实例化该单元(在cellForRowAtIndexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

我试图用设置样式类型:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                   reuseIdentifier:@"Cell"] autorelease];
}

(我的xcode在自动发布时发出了ARC警告,因此我也尝试了省略它。尽管结果相同)

我有点困惑。 显然,如果没有cell == nil ,那么代码的第一部分是徒劳的,但是有了它,该单元格就永远不会显示detailTextLabel了。 (是的,正确设置了cell.detailTextLabel.text

我将如何处理?

更新:当我使用情节提要时,通过将单元格样式设置为“字幕”,我能够达到所需的结果。 但是,如何以编程方式进行操作的问题仍然存在

以编程方式执行此操作时,应更改为以下代码。 (非常感谢mbm29414)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                                  reuseIdentifier:@"Cell"];
}

创建表单元格的现代方法是注册该单元格,然后使用索引路径将其出队。 问题是,如果您注册UITableViewCell ,将始终获得类型为default的单元格。

解决方案是子类化UITableViewCell并在其中设置样式。 例如:

class SubtitleTableViewCell: UITableViewCell {

  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError()
  }
}

现在,在注册时使用子类。

let table = UITableView(frame: .zero, style: .plain)
table.register(DebugTableViewCell.self, forCellReuseIdentifier: identifier)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM