Table View

Discussion in 'iPhone/iPad Programming' started by forrestgrant, Aug 25, 2008.

  1. macrumors member

    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.
  2. macrumors member


    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.


  3. macrumors member

    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 ;)
  4. macrumors member


    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.
  5. macrumors member

    Just what I needed, thanks!

Share This Page