ios – How to use UITableViewCell with UITableViewCellStyle with cell reuse correctly?

ios – How to use UITableViewCell with UITableViewCellStyle with cell reuse correctly?

If youre 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 Ive 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)

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.

ios – How to use UITableViewCell with UITableViewCellStyle with cell reuse correctly?

Leave a Reply

Your email address will not be published.