PDA

View Full Version : Table View




forrestgrant
Aug 25, 2008, 03:39 PM
I have a table view, and cells are set up differently based on which section they are in. The trouble is, when I scroll, it wants to reset each cell that is off screen. This makes it crash if say, cell 0, 1, 2 are off screen then I scroll up, and it tries to put section 1 cell 1 into section 2 cell 1, and everything breaks.

I apologize for this being vague, but I am counting on someone having the exact same problem, I think it is relatively common.



davedelong
Aug 25, 2008, 03:58 PM
Yep, that's because the iPhone is trying to be smart about loading cells. When a cell is scrolled offscreen, it gets put into the reuse queue. Then when it comes time to draw a new cell, it looks up in the reuse queue to see if there are any available for a particular type.

Most likely you left the default identifier as "MyIdentifier". If you want to have a different kind of cell for each section, you need to give them different identifiers. You can determine this based off the NSIndexPath that's passed into the cellForRow datasource method.

That way, the iPhone will only look for cells with the appropriate identifier when it wants to reuse them.

HTH,

Dave

Taum
Aug 25, 2008, 04:48 PM
I don't think you *have* to reuse cells. You could simply create new cells and not bother with reusing the old ones. But then you probably lose the performance gain for not re-creating every cell. As always, it's up to you to choose ;)

davedelong
Aug 25, 2008, 04:50 PM
I don't think you *have* to reuse cells. You could simply create new cells and not bother with reusing the old ones. But then you probably lose the performance gain for not re-creating every cell. As always, it's up to you to choose ;)

Correct. You don't have to reuse old cells. The reuse queue is even smart enough that it won't store more than a certain number of cells.

The idea is that alloc'ing new cells can be somewhat expensive, and this is a way to get around that.

forrestgrant
Aug 26, 2008, 09:55 AM
Just what I needed, thanks!