6

I want to use UITableViewCellStyle.Subtitle style for the default table cells. I found an answer in an SO answer like this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell?
    if (cell != nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
    }
}

With the code above, I can successfully use the Subtitle cell style. However, I start to think something might be wrong? Why create a new cell when cell != nil? In this way, you never reuse cells, isn't it? Besides, I can just call

let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

I am having the same result. Why dequeue reusable cell & then create a new one? What is the right way to achieve cell reuse & at the time, to use UITableViewCellStyle.Subtitle style for the cells?

UPDATE

Please note the first block of code is cell != nil, not cell == nil. If I change cell == nil, the code will not use the Subtitle style. I think the first works because it always create new cells with Subtitle style.

Community
  • 1
  • 1
Joe Huang
  • 6,296
  • 7
  • 48
  • 81
  • I think you're correct that it wouldn't reuse cells. What happens if you change it to `cell == nil`. – Craig Siemens Feb 21 '16 at 06:25
  • If I change `cell == nil`, the cells are not using `UITableViewCellStyle.Subtitle` style – Joe Huang Feb 21 '16 at 06:28
  • Are you registering a nib/class with your tableview? Do you have a prototype cell in a storyboard? – Craig Siemens Feb 21 '16 at 06:36
  • I don't have a nib/class for the cells. I register like this `tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")`, maybe it's somewhere to set the style? – Joe Huang Feb 21 '16 at 06:42

2 Answers2

16

If you're using tableView.registerClass, there is no way to override the style that it will pass to the class when each cell is created. A workaround I've used is to create a UITableViewCell subclass called SubtitleCell that always uses the .subtitle style.

import UIKit

class SubtitleCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
         super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
    }
}

Then I register that class with the tableView

tableView.register(SubtitleCell.self, forCellReuseIdentifier: "cell")
jvarela
  • 3,744
  • 1
  • 22
  • 43
Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • hm... it's interesting... I think the answer about setting the style & cell resue at the same time is actually wrong then. It simply creates new cells. – Joe Huang Feb 21 '16 at 07:22
1

You are basically right; you dont need the second part of the code.

this:

let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

will give you a "new" cell if there is none to reuse or an existing one otherwise.

locrizak
  • 12,192
  • 12
  • 60
  • 80
Kutu
  • 161
  • 1
  • 8
  • Are you sure? I think this will always create a new cell. The reuse identifier here is only used later by UITableView.dequeueReusableCell. If you think about it, it can't be right, there is no reference here to the table view object – Dale Jan 13 '21 at 03:20