13

Using a UITableView and its UITableViewCell(s) in Swift. I have some problem showing the detailTextLabel field of a UITableViewCell.

I already found these two helpful posts, but could not get a fully working solution: swift detailTextLabel not showing up How to Set UITableViewCellStyleSubtitle and dequeueReusableCell in Swift?

Here is the code I am using, relevant to my question:

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
}

When I try to make use of the detailTextLabel field of UITableViewCell, it does not show up. Searching on the net, I understand that I have to use the proper style(UITableViewCellStyle) for the cell, but I do not see how to integrate that change into my code. The examples I've seen are based on subclassing UITableViewCell and I suppose I do not need to subclass UITableViewCell only to make use of the detailTextLabel field. Please tell me if I am wrong.

I have also tried a few things from the posts mentioned above, but nothing works as I want.

Community
  • 1
  • 1
Michel
  • 10,303
  • 17
  • 82
  • 179
  • 1
    You need the code in the 2nd link you posted but only after you remove the use of the `theTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")` line. – rmaddy Dec 05 '15 at 04:24

4 Answers4

43

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Patrick
  • 2,035
  • 17
  • 27
  • 2
    I wish I cud have given u +10..! Thank you..! – AnxiousMan Apr 19 '16 at 09:34
  • `tableView.dequeueReusableCellWithIdentifier` always returns nil in better case, in worse case `detailTextLabel` is nil – Gargo Nov 13 '17 at 19:16
  • 1
    got solved. register tableview cell take cell = UITableViewCell() never call – kemdo Jul 31 '18 at 15:47
  • Upvoted. There is nothing wrong with this approach and while it's not a hack, it just _looks_ like a hack, – mfaani Oct 16 '19 at 21:18
  • If you have a custom cell then all you have to do is: `var cell = tableView.dequeueReusableCellWithIdentifier("customReuseIdentifier") if cell == nil { cell = CustomCell(style: .Value1, reuseIdentifier: "customReuseIdentifier") }` – mfaani May 15 '20 at 18:00
5

If you are using Storyboard:

  1. select a cell
  2. go to attributes inspector
  3. click style
  4. choose Subtitle
stackich
  • 3,607
  • 3
  • 17
  • 41
1

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)
}
Sami
  • 669
  • 5
  • 20
0

Great answer, thank you! The only thing I'd add is that if you want a different style of detail view (I was looking for .subtitle) you'll want to do the following:

cell = UITableViewCell(style: .subtitle, reuseIdentifier: "reuseIdentifier")

where: CellStyle : Int

    case `default` = 0

    case value1 = 1

    case value2 = 2

    case subtitle = 3