Advertisement
Don_Mag

reloadData test

Feb 10th, 2022
1,398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.12 KB | None | 0 0
  1. //
  2.  
  3. class TestTableViewController: UITableViewController {
  4.    
  5.     var myData: [Int] = []
  6.    
  7.     override func viewDidLoad() {
  8.         super.viewDidLoad()
  9.        
  10.         // fill myData with 1000, 1100, 1200, 1300, 1400
  11.         myData = (0..<5).map { 1000 + ($0 * 100)  }
  12.        
  13.         tableView.register(UITableViewCell.self, forCellReuseIdentifier: "c")
  14.     }
  15.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  16.         return myData.count
  17.     }
  18.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  19.         // output the row and the data for that row
  20.         print("r:\t", indexPath.row, "\t", myData[indexPath.row], "\tin cellForRowAt")
  21.        
  22.         let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath)
  23.         c.textLabel?.text = "\(myData[indexPath.row])"
  24.         return c
  25.     }
  26.     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  27.         self.myReload()
  28.     }
  29.    
  30.     func myReload() {
  31.        
  32.         print()
  33.         print("on entry to myReload(), myData is")
  34.         print()
  35.        
  36.         // output the updated data
  37.         print(myData)
  38.        
  39.         print()
  40.         print("now call .reloadData()")
  41.        
  42.         // call .reloadData here
  43.         tableView.reloadData()
  44.        
  45.         print()
  46.         print("now modify data")
  47.         print()
  48.        
  49.         for i in 0..<myData.count {
  50.             myData[i] = myData[i] + 10
  51.         }
  52.        
  53.         print("myData is now")
  54.         print()
  55.        
  56.         // output the updated data
  57.         print(myData)
  58.        
  59.         print()
  60.         print("which is shown in the table, even though we updated the data *after* calling .reloadData()")
  61.         print()
  62.  
  63.     }
  64.  
  65. }
  66.  
  67. // output is:
  68.  
  69. /*
  70.  
  71. r:   0   1000   in cellForRowAt
  72. r:   1   1100   in cellForRowAt
  73. r:   2   1200   in cellForRowAt
  74. r:   3   1300   in cellForRowAt
  75. r:   4   1400   in cellForRowAt
  76.  
  77. on entry to myReload(), myData is
  78.  
  79. [1000, 1100, 1200, 1300, 1400]
  80.  
  81. now call .reloadData()
  82.  
  83. now modify data
  84.  
  85. myData is now
  86.  
  87. [1010, 1110, 1210, 1310, 1410]
  88.  
  89. which is shown in the table, even though we updated the data *after* calling .reloadData()
  90.  
  91. r:   0   1010   in cellForRowAt
  92. r:   1   1110   in cellForRowAt
  93. r:   2   1210   in cellForRowAt
  94. r:   3   1310   in cellForRowAt
  95. r:   4   1410   in cellForRowAt
  96.  
  97. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement