UITableViewCell detailTextLabel not showing up
You've registered the UITableViewCell
theTableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "reuseIdentifier")
This means that when you call
tableView.dequeueReusableCellWithIdentifier("reuseIdentifier",
forIndexPath: indexPath)
one will be automatically created for you using the UITableViewCellStyleDefault style.
Because you want a custom style you need to do a few things:
Remove
theTableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "reuseIdentifier")
Replace
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier",
forIndexPath: indexPath)
with the following
var cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
if cell == nil {
cell = UITableViewCell(style: .Value1, reuseIdentifier: "reuseIdentifier")
}
What is happening here is dequeueReusableCellWithIdentifier
will return nil if it can't dequeue a cell. You can then generate your own cell providing with the specified style.
If you are using Storyboard
:
- select a cell
- go to attributes inspector
- click style
- choose
Subtitle
I found that it's not enough to check if the cell
is nil.
I added this additional check:
var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath)
if cell == nil || cell?.detailTextLabel == nil {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellReuseIdentifier)
}