I am doing a programmatic Swift app and can't find a way to add style: .subtitle to the cell when I am using tableView.dequeueReusableCell.
In the following code I want to be able to set the style to .subtitle:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
...
return cell
}
I tried adding the following but it cannot be used in conjunction with dequeue:
var cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
What is the correct way to add this?
CodePudding user response:
Subclass UITableViewCell
, and override init(style:reuseIdentifier:)
:
class MyTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
}
then, register this class with the table view:
tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cellIdentifier")