I saw this question:
UISwitch in a UITableView cell
But it seemed to be dealing with a dynamic page. I'm really just trying to create a settings page for my app and a few of the cells in the table (not all of them), require switches on the table cell. How could I do this?
Here's how it works in Interface Builder. No need for code.
Drag a UISwitch into your View Controller. It needs to be outside the main view. Kinda just chillin' there with some other things.
Ctrl-Click on the table view cell that should have this switch, and drag to the new Switch. See 1.
Connect the switch to the table view cell's accessoryView outlet. See 2.
Run the app and watch the switch appear in the table - done!
Note: the switch oddly does not show up in Interface builder. But it will show up when you run the app
If you also want to hook it up to an instance variable - for example so you can read its "on" property later - connect it to an outlet just like you would any other view 3, 4.
PS: Keep in mind that UISwitch on/off state is in its "on" property, not "selected". I am just adding this because it's kinda confusing.
PPS: Sorry for the oversized pix, I'm on a retina screen.
Just drag a UISwitch over to the cell that you have laid out. You can then use the assistant editor (which is awesome) to wire it up to the parent class. One tip I will give ya, if you are planning several cells that will be similar in appearance, create the first one in the section and lay it out just right, then you can increase the # of rows in that section and IB will create copies of the existing row for you.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With