Мой тестовый пример выглядит следующим образом:< /p>
Код: Выделить всё
import UIKit
import SnapKit
class ViewController: UIViewController {
var list: [dao] = [dao]()
private lazy var tableView: UITableView = {
let view = UITableView()
view.estimatedRowHeight = 44
view.rowHeight = UITableView.automaticDimension
view.register(ListCell.self, forCellReuseIdentifier: "listCell")
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
list.append(dao(featured: "featured1", name: "featured1"))
list.append(dao(featured: "featured2", name: "featured2"))
self.view.addSubview(tableView)
tableView.snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.top.equalToSuperview()
make.bottom.equalToSuperview()
}
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath) as? ListCell {
let row: dao = self.list[indexPath.row]
cell.update(row: row, idx: indexPath.row + 1)
return cell
}
return UITableViewCell()
}
}
class ListCell: UITableViewCell {
let featruedIV: UIImageView = {
let view: UIImageView = UIImageView()
view.contentMode = .scaleAspectFill
view.layer.cornerRadius = 6
view.clipsToBounds = true
view.isUserInteractionEnabled = true
return view
}()
let nameLbl: UILabel = {
let view = UILabel()
return view
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.setupView()
}
override func awakeFromNib() {
super.awakeFromNib()
setupView()
}
private func setupView() {
self.addSubview(featruedIV)
featruedIV.snp.makeConstraints { make in
make.top.equalToSuperview().offset(12)
make.left.equalToSuperview().offset(12)
make.bottom.equalToSuperview().offset(-12)
make.height.width.equalTo(90)
}
let tg: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(pressed))
tg.cancelsTouchesInView = true
featruedIV.addGestureRecognizer(tg)
self.addSubview(nameLbl)
nameLbl.snp.makeConstraints { make in
make.centerY.equalTo(featruedIV.snp.centerY)
make.left.equalTo(featruedIV.snp.right).offset(12)
}
}
func update(row: dao, idx: Int) {
nameLbl.text = row.name
featruedIV.image = UIImage(named: row.featured)
}
@objc func pressed() {
print("pressed")
}
}
class dao {
var featured: String
var name: String
init(featured: String, name: String) {
self.featured = featured
self.name = name
}
}
[img]https: //i.sstatic.net/BOclm8Qz.png[/img]
Подробнее здесь: https://stackoverflow.com/questions/785 ... leviewcell