Я не очень часто использую раскадровки, но я знаю, что это работает. Сначала вы должны зарегистрировать ячейку в вашем viewDidLoad:
tableView.register(ScheduleDisplayStartTimeCustomCell.self, forCellReuseIdentifier: "displayStartTime")
Затем вы можете программно создать пользовательскую ячейку, например, такую:
class ScheduleDisplayStartTimeCustomCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let startLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 1
label.textAlignment = .left
return label
}()
let timeLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 1
label.textAlignment = .right
return label
}()
func setupView() {
addSubview(startLabel)
addSubview(timeLabel)
NSLayoutConstraint.activate([
startLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0),
startLabel.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15),
timeLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -15),
timeLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0),
])
self.selectionStyle = UITableViewCell.SelectionStyle.gray
}
}
И, наконец, вы должны настроить свои ячейки следующим образом:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "displayStartTime") as! ScheduleDisplayStartTimeCustomCell
cell.startLabel.text = "Start"
cell.timeLabel.text = startTime
return cell
}