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.