0

I have a custom UITableViewCell that has data that I get from the server. I set the reuseIdentifier of the cell when the data comes in. Everything works fine, until I do a pull down to refresh and get new/updated data. The identifier from the server is the same, but the data may be different (which is an expected result in this case). When this happens I need to re-create the cells, and keep the same reuseIdentifier. I know that not setting the reuseIdentifier is one way around this, but that's a very bad idea, that hurts performance.

I've looked at plenty of question here involving reuseIdentifier's, but none of them seam to answer my question.

Thank you for any insight you have!

The Guardian
  • 374
  • 1
  • 7
  • 22
  • I don't think you understand how it's supposed to work. A reuseIdentifier identifies a particular way of initializing a cell (eg, setting a background color, adding a label, etc), so that, if you dequeue one with that identifier, you don't need to redo the initialization. – Hot Licks Sep 30 '14 at 22:08
  • 1
    For a simple table, with all the rows formatted the same, a single reuseIdentifier value is used for everything. – Hot Licks Sep 30 '14 at 22:15
  • I know that the reuseIdentifier identifies a particular way of identifying a cell. In my case, each cell is uniquely created based off the information I get from the server. So when I scroll to the bottom, and back up, I do get the correct cell, because the reuseIdentifier is set, and I don't need to re init it. When I do a pull down to refresh, the data for cell "x" may change, and the way my cells are set up, I need to recreate the cell. – The Guardian Sep 30 '14 at 22:40
  • The reuseIdentifier identifies the **format** of the cell. You need to define to yourself what constitutes "format" and what constitutes "content" (this is, to a degree, a judgment call). When you dequeue with identifier you assume that "format" is correct and you reset/redo any "content". – Hot Licks Sep 30 '14 at 22:53

1 Answers1

2

There's no need to clear the reuseIdentifier. Simply reload the table view after setting up your new data. All visible cells will be reloaded. Using the same reuseIdentifier is fine. As long as your cellForRowAtIndexPath method is using the new data to populate each cell, you will get the desired results.

Update - The comment by mkral is a good clarification. The reuseIdentifier represents the type of cell, not the data. So the reuseIdentifier should have nothing to do at all with the identifier from the server unless the server's identifier affects the type of cells being shown.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • To add to this, `reuseIdentifier` is meant to identify different types of cells. So if you have only one type of cell it is checking to see if it can reuse another cell with the same identifier that is no longer used (off screen) so it doesn't create 1000 instances of UITableViewCell for 1000 records. It would only create +/- the number of cells that can be visible for the tableview and reuse the non-visible ones as you scroll – mkral Sep 30 '14 at 22:07
  • Each cell that I have is uniquely created based off the server information, when I init the cell, I init it with the data from the server. When reload data gets called, the table view dequeues the cell, and the changes don't show up, because the init doesn't get called. – The Guardian Sep 30 '14 at 22:45
  • 1
    You need to update your question with your `cellForRowAtIndexPath` method. BTW - the data should not be passed to the cell's `init...` method. Cell's get reused. You need to be able to update the cell's data after the call to `init`. – rmaddy Sep 30 '14 at 22:53
  • @TheGuardian - You don't `init` cells with their variable content. You create a cell "empty" and set the variable content in `cellForRowAtIndexPath`. – Hot Licks Sep 30 '14 at 22:56
  • I changed my data structure so the data gets set in the cellForRowAtIndexPath. Thanks guys. – The Guardian Oct 01 '14 at 16:57