Причина в том, что вы используете одну ячейку экземпляра для всего представления таблицы.Это неправильный способ загрузки ваших данных в ваш UITableView
Но вы можете попробовать использовать вместо:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var index = indexPath.row
if contentData.count > 0 {
cellTextLabel.text = contentData[index].title
cellDetailLabel.text = contentData[index].text
return cell
} else {
return cell
}
}
Попробуйте использовать так:
class MyCell: UITableViewCell {
let cellTextLabel = UILabel()
let cellDetailLabel = UILabel()
let imageView = UIImageView()
let phImage = UIImage(named: "CryptiXJustX")
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(imageView)
imageView.backgroundColor = .clear
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.widthAnchor.constraint(equalToConstant: 80).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
imageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
contentView.addSubview(cellTextLabel)
cellTextLabel.translatesAutoresizingMaskIntoConstraints = false
cellTextLabel.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10).isActive = true
cellTextLabel.rightAnchor.constraint(equalTo: contentView.contentView.rightAnchor, constant: -10).isActive = true
cellTextLabel.topAnchor.constraint(equalTo: contentView.contentView.topAnchor, constant: 10).isActive = true
cellTextLabel.textColor = .white
contentView.addSubview(cellDetailLabel)
cellDetailLabel.translatesAutoresizingMaskIntoConstraints = false
cellDetailLabel.leftAnchor.constraint(equalTo: imageView.rightAnchor, constant: 10).isActive = true
cellDetailLabel.rightAnchor.constraint(equalTo: contentView.contentView.rightAnchor, constant: -10).isActive = true
cellDetailLabel.topAnchor.constraint(equalTo: cellTextLabel.bottomAnchor, constant: 10).isActive = true
cellDetailLabel.textColor = .white
cellDetailLabel.font = cellDetailLabel.font.withSize(12)
cellDetailLabel.numberOfLines = 3
contentView.backgroundColor = .clear
contentView.textLabel?.textColor = .white
contentView.detailTextLabel?.textColor = .white
}
}
На вас ViewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// if you are not using storyboard, make sure you are registered the cell ID
self.tableView!.register("CellID", forCellReuseIdentifier: tableReuseIdentifier)
}
Наконец:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: 'CellID', for: indexPath) as! MyCell
cell.imageView.image = phImage
cell.cellTextLabel.text = contentData[indexPath.row].title
cell.cellDetailLabel.text = contentData[indexPath.row].text
return cell
}