class TableViewReloadTestViewController: UIViewController {
private lazy var tableView: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
tableView.frame = view.frame
view.addSubview(tableView)
tableView.dataSource = self
tableView.delegate = self
}
}
extension TableViewReloadTestViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellForItemAt \(indexPath)")
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = "Row \(indexPath.row)"
let redView = UIView()
cell.contentView.addSubview(redView)
redView.backgroundColor = .red.withAlphaComponent(0.1)
redView.translatesAutoresizingMaskIntoConstraints = false
cell.contentView.addConstraints([
.init(item: redView, attribute: .left, relatedBy: .equal, toItem: cell.contentView, attribute: .left, multiplier: 1.0, constant: 8.0),
.init(item: redView, attribute: .right, relatedBy: .equal, toItem: cell.contentView, attribute: .right, multiplier: 1.0, constant: -8.0),
.init(item: redView, attribute: .top, relatedBy: .equal, toItem: cell.contentView, attribute: .top, multiplier: 1.0, constant: 8.0),
.init(item: redView, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView, attribute: .bottom, multiplier: 1.0, constant: -8.0)
])
let heightConstraint = NSLayoutConstraint(item: redView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100.0)
heightConstraint.priority = .defaultLow
redView.addConstraint(heightConstraint)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//let randomRow = (0..<tableView.numberOfRows(inSection: indexPath.section)).randomElement()!
// let's reload the selected row (Visible row)
let randomRow: Int = indexPath.row
tableView.reloadRows(at: [IndexPath(row: randomRow, section: indexPath.section)], with: .automatic)
print("Reloading at \(randomRow)")
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
// log the calls
print("estimatedHeightForRowAt ", indexPath)
return 10
}
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎