Search code examples
objective-cuitableviewuiswitch

How do i get the effect of a UISwitch as the accessory for a UITableViewCell?


I have a working UITableView filled with some options for my app, and i want to add UISwitches to them just like the Settings app, how do i do this?

Thanks :)


Solution

  • You can create your own custom accessory view. When handling tableView:cellForRowAtIndexPath:, just create a switch in the row cell like this:

    UISwitch* turboSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 84.0, 27.0)];
    [turboSwitch setOn:[engine.turbo boolValue]];
    cell.accessoryView = turboSwitch;
    cell.textLabel.text = @"Turbo";
    

    Don't forget to connect the target action to your controller.