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];
}