Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell detailTextLabel not showing up

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.

like image 896
Michel Avatar asked Dec 05 '15 03:12

Michel


3 Answers

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.

like image 165
Patrick Avatar answered Oct 19 '22 21:10

Patrick


If you are using Storyboard:

  1. select a cell
  2. go to attributes inspector
  3. click style
  4. choose Subtitle
like image 30
stackich Avatar answered Oct 19 '22 19:10

stackich


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)
}
like image 1
Sami Avatar answered Oct 19 '22 19:10

Sami