Advertisement
Don_Mag

Untitled

Oct 12th, 2023
1,435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.03 KB | None | 0 0
  1. class MultilineLabelCell: UITableViewCell {
  2.     let label = UILabel()
  3.    
  4.     override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  5.         super.init(style: style, reuseIdentifier: reuseIdentifier)
  6.         commonInit()
  7.     }
  8.     required init?(coder: NSCoder) {
  9.         super.init(coder: coder)
  10.         commonInit()
  11.     }
  12.     private func commonInit() {
  13.         label.translatesAutoresizingMaskIntoConstraints = false
  14.         contentView.addSubview(label)
  15.         let g = contentView.layoutMarginsGuide
  16.         NSLayoutConstraint.activate([
  17.             label.topAnchor.constraint(equalTo: g.topAnchor),
  18.             label.leadingAnchor.constraint(equalTo: g.leadingAnchor),
  19.             label.trailingAnchor.constraint(equalTo: g.trailingAnchor),
  20.             label.bottomAnchor.constraint(equalTo: g.bottomAnchor),
  21.         ])
  22.         label.numberOfLines = 0
  23.         // so we can see the framing
  24.         label.backgroundColor = .yellow
  25.     }
  26.    
  27. }
  28.  
  29. class ReloadRowsTestVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
  30.    
  31.     var myData: [String] = []
  32.     var reloadCount: Int = 0
  33.    
  34.     let tableView = UITableView()
  35.    
  36.     override func viewDidLoad() {
  37.         super.viewDidLoad()
  38.        
  39.         // some sample data
  40.         for i in 1..<50 {
  41.             let nLines = i % 4
  42.             var s: String = "Row: \(i-1)\nLine 1"
  43.             for n in 0..<nLines {
  44.                 s += "\nLine \(n+2)"
  45.             }
  46.             myData.append(s)
  47.         }
  48.         myData[4] += "\nReload Count: 0"
  49.  
  50.         var cfg = UIButton.Configuration.filled()
  51.         cfg.title = "Reload row 4"
  52.  
  53.         let btn = UIButton(configuration: cfg, primaryAction: UIAction() { _ in
  54.             self.reloadCount += 1
  55.             self.myData[4] += "\nRow 4 - Reload Count: \(self.reloadCount)"
  56.             print("Reload Count:", self.reloadCount)
  57.             self.tableView.reloadRows(at: [IndexPath(row: 4, section: 0)], with: .none)
  58.         })
  59.  
  60.         btn.translatesAutoresizingMaskIntoConstraints = false
  61.         view.addSubview(btn)
  62.        
  63.         tableView.translatesAutoresizingMaskIntoConstraints = false
  64.         view.addSubview(tableView)
  65.        
  66.         let g = view.safeAreaLayoutGuide
  67.         NSLayoutConstraint.activate([
  68.            
  69.             btn.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  70.             btn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
  71.             btn.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
  72.            
  73.             tableView.topAnchor.constraint(equalTo: btn.bottomAnchor, constant: 20.0),
  74.             tableView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  75.             tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  76.             tableView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
  77.            
  78.         ])
  79.        
  80.         tableView.register(MultilineLabelCell.self, forCellReuseIdentifier: "c")
  81.         tableView.dataSource = self
  82.         tableView.delegate = self
  83.  
  84.     }
  85.    
  86.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  87.         return myData.count
  88.     }
  89.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  90.         print("cellForRowAt:", indexPath)
  91.         let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath) as! MultilineLabelCell
  92.         c.label.text = myData[indexPath.row]
  93.         return c
  94.     }
  95.    
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement