I create a function for this case and its work good for me .. try it , first in the cell creation you could check the row you want to add a UISwitch
in , for ex.
if(indexPath.row == 0)
[self createOnOffView:cell withTitle:@"Somthing" withTag:1001 defaultVal:YES];
And the function is :
- (void) createOnOffView:(UITableViewCell*) cell withTitle:(NSString*) title withTag:(int)tag defaultVal:(BOOL) defaultVal
{
CGRect rect;
cell.textLabel.text = title;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
rect = cell.contentView.frame;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
rect.origin.x = cell.frame.size.width - 20;
else
rect.origin.x = rect.size.width/2 +35;
rect.origin.y = rect.size.height/2 - 15;
rect.size.width = 60;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:rect];
[cell.contentView addSubview:switchView];
[switchView addTarget:self action:@selector(didChangeSwitch:) forControlEvents:UIControlEventValueChanged];
switchView.tag = tag;
[switchView setOn:defaultVal];
[switchView release];
}
And when the value is switch is change this method will be fired .. so you can know which switch based on the tag
- (void) didChangeSwitch:(UISwitch*)switchView
{
if(switchView.tag == 1001)
{
//Do Somthing
}
if(switchView.tag == 1002)
{
//Do Somthing
}
}
Hope this will be helpful :)