I have a UITableView that should only display a UISwitch in the first section, first row, but for some reason, the switch is being displayed in the 4th section. Also, when ever the switch is flipped on/off, a new UISwitch appears in another cell. It is acting very strangely. Any help would be greatly appreciated.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
PrettyTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}
[cell prepareForTableView:tableView indexPath:indexPath];
cell.cornerRadius = 10;
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Push Notifications";
pushNotificationSwitch = [[UISwitch alloc] init];
pushNotificationSwitch.on = pushSwitchState;
cell.accessoryView = pushNotificationSwitch;
[pushNotificationSwitch addTarget:self action:@selector(pushSwitch:) forControlEvents:UIControlEventValueChanged];
cell.userInteractionEnabled = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Department Based";
if (departmentBased) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.userInteractionEnabled = YES;
}
if (indexPath.row == 1) {
cell.textLabel.text = @"Location Based (GPS)";
if (locationBased) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
} cell.userInteractionEnabled = YES;
}
}
if (departmentBased) {
if (indexPath.section == 2) {
if (indexPath.row == 0) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 1) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 2) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 3) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 4) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 5) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 6) {
cell.textLabel.text = @"xxxxxx";
}
}
if (indexPath.section == 3) {
if (indexPath.row == 0) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 1) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 2) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 3) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 4) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 5) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 6) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 7) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 8) {
cell.textLabel.text = @"xxxxxx";
}
}
}
return cell;
}
You are experiencing the effects of cell reusability in iOS. You are using the same cell identifier for all cells, so when you scroll, an old cell will become available and dequeued from the tableView
.
Try to set cell.accessoryView
to nil right after:
if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}
cell.accessoryView = nil;
Alternatively, pick a different cell identifier for that first indexPath
, making sure you take that into account when dequeuing.
BOOL firstCell = indexPath.row == 0 && indexPath.section == 0;
NSString *CellIdentifier = firstCell ? @"CellWithSwitch" : @"Cell";
PrettyTableViewCell *cell =
[tableViewdequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}