I have a problem with uiswitch in my UITableViewCell
that whenever i change a switch value in a specific cell that belongs to a specific section. All other sections
As already pointed out by others, you're adding multiple switches to each cell. FTFY:
UISwitch *switchController = nil;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
switchController.tag = 'swch';
CGRect switchFrame = switchController.frame;
//set its x and y value, this you will have to determine how to space it on the left side
switchFrame.origin.x = 50.0f;
switchFrame.origin.y = 10.0f;
switchController.frame = switchFrame;
[switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switchController];
}
else
{
switchController = (UISwitch*)[cell.contentView viewWithTag: 'swch'];
NSAssert(switchController != nil, @"how?");
}
NSString *str = [[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
NSLog(@"string in cell%@", str);
switchController.on = [str isEqualToString:@"ON"];
return cell;