views:

460

answers:

3

I am having an issue with how the iPhone reuses Cells in table. The problem is that implemented methods of the class also get "reused" and changes in one cell get applied to other cells that were reused. I have a progress indicator that should only be updating in one cell after user interaction, but the progress indicator updates and the method runs inside the 6th cell down too. It happens on the 2nd and 7th cell, also.

I know it's bad for memory to not reuse any cells, but in this app there are never more than 7 or so cells anyway.

 CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

doesn't work. If I scroll away for a second and come back, the Cell is back to normal, and all updates that have happened are reset. The cell destroyed itself and was drawn again, I guess.

I need to know if there's a quick, painless way to make the iPhone just draw every single cell individually without reusing any. Is there a way to maybe use fast iteration or a for loop to create an individual cell for each line in the dataSource?

Please let me reiterate, I'm not ever going to have any more than 7 or 8 cells in one table on a given viewController.

+1  A: 

If you really want to do it… setup an Array in viewDidLoad that is 7 or 8 long. Do a [[UITableView alloc] initWithStyle:UITabeViewCellStyle… reuseIdentifier:@"whatever"] 7 or 8 times, then in cellForRowAtIndexPath, return the cell from the array. You should make sure to release the array of cells in the dealloc method.

coneybeare
I guess I forgot to say that I'm loading these cells from a nib file. I found a great tutorial on doing this at the iCodeBlog. The cells are really customized and have quite a bit of info. To use your method I need to layoutSubviews in code. I'll try tomorrow, I'm really tired right now.
JustinXXVII
You wont have to do layoutSubviews. The only difference is that you are not creating them on-demad, but creating them ahead of time. cellForRowAtIndexPath just asks that you return a cell, and that is what you are doing.
coneybeare
You might not literally be calling the initWithStyle:reuseIdentifier: initializer. You'll be allocating the cells in exactly the same way you would if dequeueReusableCellWithIdentifier: returned nil. But the main point is, *don't* call dequeueReusableCellWithIdentifier:.
Sixten Otto
A: 

If you only use 7 rows - reuseIdentifyer is useless. How does it work? Imagine that you are scrolling some table with many rows down. There is a moment, when one of the rows shold be hidden and another one will be shown. Only then the table gets cell by reusecellidentifyer. It doesn't allocate memory for another cell. It takes you hidden row and use it for the new row to display. Sorry for my english)

Morion
But if i change reuseIdentifer to nil, the table draws a cell each time the table scrolls, so the cells at the top get replaced when i scroll down and then back up, overwriting all the cell updates in the process.
JustinXXVII
You may not use reuseIdentifyer at all if you have only 7 rows.
Morion
A: 

What finally worked for me was programatically doing the layout. Using interface builder for TableViewCells is just buggy. Creating a TableViewCell class and the layoutSubviews method with lots of CGRects and frame properties is what got it to work. Even though the cells get reused, the actual data INSIDE THE CELL doesn't overwrite.

JustinXXVII