1

When using dequeueReusableCellWithIdentifier, I am simply unable to get a detailTextLabel in the UITableViewCell.

If I allocate a new UITableViewCell each time in cellForRowAtIndexPath, the detailTextLabel is shown, but obviously I'll get a memory leak this way.

Why is this happening, and how do I manage to show a detailTextLabel with reusable cells?

Update: I don't use Storyboard and it's a basic system cell. I realise now that dequeue... never returns nil, so where am I supposed to configure the cell as style .Subtitle?

class SomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        let bounds = UIScreen.mainScreen().bounds
        tableView = UITableView(frame: bounds, style: .Plain)
        tableView.delegate   = self
        tableView.dataSource = self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        self.view.addSubview(self.tableView)
    }

    // uses dequeue
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath);
        if cell == nil {
            cell = UITableViewCell(style: .Subtitle, reuseIdentifier: reuseIdentifier)
        }
        cell.textLabel?.text = "Text"
        cell.detailTextLabel?.text = "Detail NOT displayed"
        return cell
    }

    // doesn't use dequeue...
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: reuseIdentifier) // memory leak
        cell.textLabel?.text = "Text"
        cell.detailTextLabel?.text = "Detail displayed"
        return cell        
    }


}
forthrin
  • 2,709
  • 3
  • 28
  • 50
  • Possible duplicate of http://stackoverflow.com/questions/27221003/uitableviewcell-not-showing-detailtextlabel-text-swift – Sheereen S May 03 '16 at 08:41
  • if the cell you reuse a custom cell or basic system cell? – MudOnTire May 03 '16 at 08:45
  • The method `dequeueReusableCellWithIdentifier:forIndexPath:` returns never `nil`, so the if condition is never true. You need to initialize the cell with the appropriate style or use storyboard. – vadian May 03 '16 at 08:55
  • First, I don't use Storyboard and it's a basic system cell. Now, if `dequeue...` never returns nil, where am I supposed to initialise my cell as a `.Subtitle` style? In `viewDidLoad`? – forthrin May 03 '16 at 10:00
  • The suggested duplicate solution doesn't work (I get no subtitle). I also tried initialising a `UITableView` cell in `viewDidLoad` and dequeue it with the same `reuseIdentifier`. No subtitle then either. – forthrin May 03 '16 at 11:13

2 Answers2

3

I found the solution. I removed the registerClass line and changed to the dequeueReusableCellWithIdentifier function without the forIndexPath argument. Worked instantly!

forthrin
  • 2,709
  • 3
  • 28
  • 50
0

You have to do it this way for "dequeueReusableCellWithIdentifier".

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell : CustomCell! = tableView.dequeueReusableCellWithIdentifier("ID_CustomCell", forIndexPath: indexPath)  as! CustomCell

    cell.lblData.text = "Text"

    return cell

}
Hasya
  • 9,792
  • 4
  • 31
  • 46