user3058443
user3058443

Reputation: 1

Workaround for dequeueReusableCellWithIdentifier

I've got a tableView that initially loads one custom cell. The cell has a textfield and a segment control that the user can modify. There is also an add cell button, which if pressed adds another custom cell underneath the previous one. The user can add up to 15 of these custom cells. When the first few cells eventually go off the screen and new ones are created they are just creating the exact same cells that are in index.row positions 0, 1, 2, and so on.

For example: The user enters "Test" in the textfield of the custom cell at index.row position 0. Now when that cell goes off screen and the user adds a new cell, it's just adding that original cell with the textfield.text value of "Test" and everything. And if I modify the new cell's textfield.text value to let's say "Demo" it will change the textfield at index.row position 0 to that.

Now I know this is how dequeueReusableCell works. It grabs cells that are offscreen. But how do I get around this. And have it create a unique cell each time.

*Edit This is how I've been inserting new cells.

/* create cell and add to array */
NSString *CellIdentifier1 = @"cell";
CustomCell *customCell = (CustomCell*)[self.myTableView dequeueReusableCellWithIdentifier:CellIdentifier1];

/* save cell to array */
[self.fetchCellArray addObject:customCell];

/* Add cell in table */
NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:deleteInsertIndex inSection:0];
        [self.myTableView insertRowsAtIndexPaths:@[indexPath2] withRowAnimation:UITableViewRowAnimationAutomatic];

Upvotes: 0

Views: 139

Answers (2)

SDJMcHattie
SDJMcHattie

Reputation: 1699

I'm not sure you're properly separating your data from your views. You should have an array of the text entries in each of the cells so that, in [tableView:(TableView*)tableview cellForRowAtIndexPath:(NSIndexPath*)indexPath] you can populate the cells with the text you are storing in the array. When the user updates some text, you update the array. When you dequeue a cell, assume it's dirty and already has some text in it, so instead add a new empty string entry to the end of the array as well and set the text of the new cell to an empty string. That's all there is to it.

  • Assume all dequeued cells are recycled so everything about them needs to be set to defaults
  • Keep your data for the cells in a separate object like an array so that you can setup any given cell at any index path with the data you have for that index path.

Upvotes: 0

Léo Natan
Léo Natan

Reputation: 57040

You should not store cells inside your array. You should store the backing data of the cells into an array (or any other storage you deem fit), and update your dequeued cells with the data from the array.

For example, if the user adds "Test", you will add it in the array at index 0. Now user adds "Demo", you add it at index 1 in the array. Now the user adds more and more strings, you add them all to the array. Once the user scrolls to the top, and you are asked to provide a cell for section 0 row 1, you will dequeue a cell - any cell - and set it's label according to the value of the array at index 1. Like wise for cell at section 0 index 0 - the array will return "Test".

Upvotes: 2

Related Questions