Advertisement
Don_Mag

Untitled

Oct 13th, 2023
1,118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.42 KB | None | 0 0
  1. class TableViewReloadTestViewController: UIViewController {
  2.    
  3.     private lazy var tableView: UITableView = UITableView()
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         tableView.frame = view.frame
  9.         view.addSubview(tableView)
  10.         tableView.dataSource = self
  11.         tableView.delegate = self
  12.     }
  13.    
  14. }
  15.  
  16. extension TableViewReloadTestViewController: UITableViewDataSource, UITableViewDelegate {
  17.    
  18.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  19.         100
  20.     }
  21.    
  22.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  23.        
  24.         print("cellForItemAt \(indexPath)")
  25.  
  26.         let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
  27.        
  28.         cell.textLabel?.text = "Row \(indexPath.row)"
  29.        
  30.         let redView = UIView()
  31.         cell.contentView.addSubview(redView)
  32.         redView.backgroundColor = .red.withAlphaComponent(0.1)
  33.         redView.translatesAutoresizingMaskIntoConstraints = false
  34.         cell.contentView.addConstraints([
  35.             .init(item: redView, attribute: .left, relatedBy: .equal, toItem: cell.contentView, attribute: .left, multiplier: 1.0, constant: 8.0),
  36.             .init(item: redView, attribute: .right, relatedBy: .equal, toItem: cell.contentView, attribute: .right, multiplier: 1.0, constant: -8.0),
  37.             .init(item: redView, attribute: .top, relatedBy: .equal, toItem: cell.contentView, attribute: .top, multiplier: 1.0, constant: 8.0),
  38.             .init(item: redView, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView, attribute: .bottom, multiplier: 1.0, constant: -8.0)
  39.         ])
  40.         let heightConstraint = NSLayoutConstraint(item: redView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100.0)
  41.         heightConstraint.priority = .defaultLow
  42.         redView.addConstraint(heightConstraint)
  43.        
  44.         return cell
  45.     }
  46.    
  47.     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  48.        
  49.         //let randomRow = (0..<tableView.numberOfRows(inSection: indexPath.section)).randomElement()!
  50.        
  51.         // let's reload the selected row (Visible row)
  52.         let randomRow: Int = indexPath.row
  53.        
  54.         tableView.reloadRows(at: [IndexPath(row: randomRow, section: indexPath.section)], with: .automatic)
  55.        
  56.         print("Reloading at \(randomRow)")
  57.     }
  58.    
  59.     func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  60.         // log the calls
  61.         print("estimatedHeightForRowAt ", indexPath)
  62.         return 10
  63.     }
  64.    
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement