0

In my tableview, I add subview into some cell. If the number of cell big than one screen's height, for example, the screen can contains 10 cells, if scroll to 11 cells, the first cell will disappear. When return to top, the first cell shows but with a subview even when it do not have cells.

Is it because the subview float?

atu0830
  • 405
  • 7
  • 15

3 Answers3

0

This is because the cells are being reused, in order to save memory. When you scroll your table view, it'll reuse the same cells that become pushed off screen. If some of them contained subviews, the subviews may also be reused and displayed at the wrong places.

If you're adding subviews to a cell's contentView, be sure to tag (tag property in UIView) the views and fetch them with [cell.contentView viewWithTag:YOUR_TAG_ID] and do whatever you want with them (i.e. remove the subview if it's not supposed to be in your new cell).

Example (where a UITextField is added in some cells):

// try to get the text field from the cell
textField = (UITextField*)[cell.contentView viewWithTag:kTABLE_CELL_TAG_TEXTFIELD];

if (textField) {
    // remove it if it already exists
    [textField removeFromSuperview];
}
Kristofer Sommestad
  • 3,061
  • 27
  • 39
  • Thanks, I try to remove it at viewWillAppear. But scroll action do NOT call viewWillAppear. And clean and redraw subview in a scroll event method looks not good. – atu0830 Oct 29 '11 at 02:21
  • 1
    The best way is to remove it in tableView:cellForRowAtIndexPath, before returning the reused cell. If you're experiencing lag when scrolling (i.e. if the subview's quite heavy or if you've got multiple subviews), you might be better of drawing the contents in a subclassed UITableViewCell' -drawRect: instead. Have a look at this question for some more info on how to do it: http://stackoverflow.com/questions/1106658/custom-draw-a-uitableviewcell – Kristofer Sommestad Oct 31 '11 at 09:06
  • Anyway, thanks again. It looks the system only cache cells with same reuseIdentifier. My code only contains limited cells, so remove or redraw still looks complex. – atu0830 Nov 04 '11 at 02:25
0

To avoid this kind of behaviour, you can remove all the views of your cell, before adding new subviews.

for (UIView *view in cell.contentView.subviews)
{
    [view removeFromSuperview];
}
NSZombie
  • 1,857
  • 14
  • 14
  • Yes, that would work too, but doesn't offer as much control over the content as tagging the views. Guess it's a matter of how much of a control freak you are... :) – Kristofer Sommestad Oct 28 '11 at 12:51
-1

Fixed by set unique reuseIdentifier for every cell, so that tableview can not reuse cell. My table view should be in 100 line, so looks good.

atu0830
  • 405
  • 7
  • 15
  • Even though this might work in your case, I really advise against it. It will result in the app consuming a lot more memory than it actually needs, which could cause unexpected issues elsewhere. And it defeats the purpose of the `UITableView` cache. If your cells are very different (in terms of layout etc), it'd be OK to use different cell identifiers. But I can't see how you would need 100 unique cell identifiers. If you do, you should probably reconsider your design... – Kristofer Sommestad Nov 04 '11 at 08:16
  • In fact, this table is a customized NSUserdefaults, some items use same view struct. for those item, I use one reuseIdentifier. The else item is fixed, so I can use another reuseIdentifier. I know your idea is good, but my requirement is enough to use above solution. sorry for the above solution looks not clear.. – atu0830 Nov 04 '11 at 14:45