Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- class TestTableViewController: UITableViewController {
- var myData: [Int] = []
- override func viewDidLoad() {
- super.viewDidLoad()
- // fill myData with 1000, 1100, 1200, 1300, 1400
- myData = (0..<5).map { 1000 + ($0 * 100) }
- tableView.register(UITableViewCell.self, forCellReuseIdentifier: "c")
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return myData.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- // output the row and the data for that row
- print("r:\t", indexPath.row, "\t", myData[indexPath.row], "\tin cellForRowAt")
- let c = tableView.dequeueReusableCell(withIdentifier: "c", for: indexPath)
- c.textLabel?.text = "\(myData[indexPath.row])"
- return c
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- self.myReload()
- }
- func myReload() {
- print()
- print("on entry to myReload(), myData is")
- print()
- // output the updated data
- print(myData)
- print()
- print("now call .reloadData()")
- // call .reloadData here
- tableView.reloadData()
- print()
- print("now modify data")
- print()
- for i in 0..<myData.count {
- myData[i] = myData[i] + 10
- }
- print("myData is now")
- print()
- // output the updated data
- print(myData)
- print()
- print("which is shown in the table, even though we updated the data *after* calling .reloadData()")
- print()
- }
- }
- // output is:
- /*
- r: 0 1000 in cellForRowAt
- r: 1 1100 in cellForRowAt
- r: 2 1200 in cellForRowAt
- r: 3 1300 in cellForRowAt
- r: 4 1400 in cellForRowAt
- on entry to myReload(), myData is
- [1000, 1100, 1200, 1300, 1400]
- now call .reloadData()
- now modify data
- myData is now
- [1010, 1110, 1210, 1310, 1410]
- which is shown in the table, even though we updated the data *after* calling .reloadData()
- r: 0 1010 in cellForRowAt
- r: 1 1110 in cellForRowAt
- r: 2 1210 in cellForRowAt
- r: 3 1310 in cellForRowAt
- r: 4 1410 in cellForRowAt
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement