I’m programming a preferences panel with a UITableView which looks like this:

The first section consists in simple rows and the second section in rows with a UISwitch as a subview.
When I change to landscape and start scrolling, the tableview behaves in a strange way:

This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell
if (indexPath.section == 0) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (indexPath.row == 0) {
cell.textLabel.text = @"Help";
}else if (indexPath.row == 1){
cell.textLabel.text = @"About us";
}
else if(indexPath.row == 2){
cell.textLabel.text = @"Send to a friend";
}
}else if(indexPath.section == 1){
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0) {
cell.textLabel.adjustsFontSizeToFitWidth = NO;
cell.textLabel.text = @"Show favs at launch";
[cell addSubview:favoritesSwitch];
}else if (indexPath.row == 1){
cell.textLabel.text = @"Open with Safari";
[cell addSubview:browserSwitch];
}
else if(indexPath.row == 2){
cell.textLabel.text = @"Remember query";
[cell addSubview:lastQuerySwitch];
}
else if(indexPath.row == 3){
cell.textLabel.text = @"Automatic keyboard";
[cell addSubview:keyboardSwitch];
}
}
return cell;
}
Thank you in advance 🙂
It could be happen because you are using the same cell identifier to access the table cell.
Try with
instead of.