I\'d like a UITableView with subtitle-style cells that use dequeueReusableCellWithIdentifier.
My original Objective-C code was
If you'd rather avoid optionality, you can make a subclass of UITableViewCell that looks something like this:
class SubtitleTableViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Then register it using:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(SubtitleTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
}
This allows your cell customization code to be really nice:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
cell.textLabel?.text = "foo"
cell.detailTextLabel?.text = "bar"
return cell
}