UITableViewCell的detailTextLabel没有显示出来。

14

在Swift中使用UITableView及其UITableViewCell(s)时,我遇到了一些问题,无法显示UITableViewCelldetailTextLabel字段。

我已经找到了这两篇有用的文章,但是没有得到一个完全可行的解决方案: swift detailTextLabel not showing up How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?

以下是与我的问题相关的代码:

override func viewDidLoad() {
    super.viewDidLoad()

………….
    theTableView = UITableView(frame: tmpFrame)
………….
    theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    theTableView.dataSource = self
    theTableView.delegate = self
    self.view.addSubview(theTableView)
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
    cell.backgroundColor = UIColor.clearColor()
    cell.textLabel?.text = "THE-TEXT-LABEL"
    cell.detailTextLabel?.text = "KIJILO" // This does not show up.
    return cell
}
当我尝试使用UITableViewCelldetailTextLabel字段时,它没有显示出来。在网上搜索后,我了解到我必须为单元格使用适当的样式(UITableViewCellStyle),但我不知道如何将这种更改集成到我的代码中。我看到的示例是基于子类化UITableViewCell,我认为我不需要子类化UITableViewCell就可以使用detailTextLabel字段,请告诉我是否错误。
我还尝试了一些以上提到的帖子中的方法,但没有达到我想要的效果。

1
你需要使用你发布的第二个链接中的代码,但是在使用之前请删除 theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier") 这一行。 - rmaddy
4个回答

43

你已经注册了UITableViewCell。

theTableView.registerClass(UITableViewCell.self,
 forCellReuseIdentifier: "reuseIdentifier")
这意味着当您调用

时,
tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", 
forIndexPath: indexPath)

使用UITableViewCellStyleDefault样式会自动为您创建一个表格单元格。

因为您想要一个自定义的样式,所以需要做一些事情:

删除

theTableView.registerClass(UITableViewCell.self, 
forCellReuseIdentifier: "reuseIdentifier")
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", 
forIndexPath: indexPath)

随后是以下内容:

var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
if cell == nil {
    cell = UITableViewCell(style: .Value1, reuseIdentifier: "reuseIdentifier")
}

这里发生的是,如果无法从队列中取出单元格,则 dequeueReusableCellWithIdentifier 将返回 nil。然后您可以使用指定的样式生成自己的单元格。


2
我希望我能给你+10..!谢谢..! - AnxiousMan
在更好的情况下,tableView.dequeueReusableCellWithIdentifier总是返回nil,在更糟糕的情况下,detailTextLabel也是nil。 - Gargo
1
问题已解决。注册 TableView Cell 时,不要调用 cell = UITableViewCell() - kemdo
点赞。这种方法没有任何问题,虽然它不是黑客技巧,但看起来像一个黑客技巧。 - mfaani
如果您有自定义单元格,那么您所要做的就是:var cell = tableView.dequeueReusableCellWithIdentifier("customReuseIdentifier") if cell == nil { cell = CustomCell(style: .Value1, reuseIdentifier: "customReuseIdentifier") } - mfaani

5

如果你正在使用Storyboard

  1. 选择一个单元格
  2. 进入属性检查器
  3. 点击样式
  4. 选择子标题

1
我发现仅检查 cell 是否为 nil 是不够的。我加入了这个额外的检查:
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
if cell == nil || cell?.detailTextLabel == nil {
    cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellReuseIdentifier)
}

1
这个并不做你认为它会做的事情。 - Wayne

0
太好了,谢谢!我想要补充的是,如果你想要不同样式的详细视图(我在寻找.subtitle),你需要执行以下操作:
cell = UITableViewCell(style: .subtitle, reuseIdentifier: "reuseIdentifier")
其中:CellStyle:Int
    case `default` = 0

    case value1 = 1

    case value2 = 2

    case subtitle = 3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接