Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

forrestgrant

macrumors member
Original poster
Jun 24, 2008
34
0
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

macrumors member
Sep 9, 2007
50
0
Right here.
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

macrumors member
Jul 28, 2008
56
0
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

macrumors member
Sep 9, 2007
50
0
Right here.
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.