3

I have a TableView whose cells content need to be updated when it's TableViewRowAction is excecuted. More precisely I want to update a UILabel inside the cell. Setting a new text to this label inside the action is no problem but when scrolling down/up and the cell gets reloaded, then this label inside the cell has the same text as before changing it.

I know that the cells get reused and that's why I want to know how to "avoid" this effect and how to update the content of the cells properly. I have already tried to call TableView.ReloadData() which seems to solve the problem but the cells appear in a completly different order which doesn't look very nice.

anton68
  • 387
  • 3
  • 14
  • You need to ensure that you update the data model as well as the cell. Show your `cellForRow` code as well as your action handler. – Paulw11 Jul 03 '18 at 20:43

3 Answers3

1

The most straightforward approach is to update the data that your UITableView is drawing from (i.e. the array of data that you're populating each cell from has been updated to reflect your text change), then reload the specific cell of the UITableView:

let indexPath = IndexPath(row: 0, section: 0)
tableView.reloadRows(at: [indexPath], with: .automatic)
MQLN
  • 2,292
  • 2
  • 18
  • 33
  • When I am reloading the affected row with `.reloadRows()`, although the update is performing well the cell is getting moved to another postion. Before reloading the row I set the source for the table. – anton68 Jul 03 '18 at 21:13
  • Perfect, so call the reloadRows `tableView.reloadRows(at: [indexPath], with: .automatic)` method at EXCLUSIVELY the row you want to reload. As long as you know the row & section of the row you need to reload, you can create the indexPath of that row, and reload it easily, without causing the WHOLE THING to reload! – MQLN Jul 03 '18 at 21:14
  • I got it. I fixed the issue that the cell was moving to a new position. The only problem left is that the `UITableView` flickers and "jumps around" for a short moment. Do you know the reason for this? – anton68 Jul 03 '18 at 21:42
  • Has it maybe something to do with the `TableViewRowAction`'s animation? – anton68 Jul 03 '18 at 22:03
  • Possible! Try `uitableView.reloadRows(at: [indexPath], with: .none)` – MQLN Jul 03 '18 at 22:04
  • Well, the issue was that I didn't set `TableView.EstimatedRowHeight`. Now there is no glitching anymore when reloading a row, but a new problem (it doesn't seem to come to an end) is that I also want to delete rows sometimes and the animation can't be excecuted properly because of the row height. – anton68 Jul 04 '18 at 18:02
  • Ahhh awesome! Nice job figuring it out! You should post your answer, and mark it as solved! – MQLN Jul 04 '18 at 18:48
  • Sure, I will do that. But it seems that you also don't know a workaround the new animation problem, right? – anton68 Jul 04 '18 at 19:03
1

As you said table cells are reused , this means you have to keep a model array for the whole table and make all the changes to it , then reload the table more importantly to set the content of the cell index in cellForRowAt

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

To achieve this you must create an Array that acts as a dataSource for your labels. When you are changing the text for a specific label, you must update the array. You can get the exact update location from the property indexPath of the method cellForRowAt. Every single time you scroll up and down, cellForRowAt method is called. Just use something like

label.text = array[indexPath.row]
Sanket Ray
  • 1,071
  • 1
  • 15
  • 24