views:

165

answers:

4

How can I embed a UISwitch on a UITableView cell? Examples can be seen in the settings menu.

My current solution:

UISwitch *mySwitch = [[[UISwitch alloc] init] autorelease];
cell.accessoryView = mySwitch;
+2  A: 

You can add a UISwitch or any other control to the cell's accessoryView. That way it will appear on the right-hand side of the cell which is probably what you want.

Echelon
A: 

You could prepare the cell in Interfacebuilder, link it to an IBOutlet of your Viewcontroller and return it when the tableview is asking for the proper row.

Instead, you could create a separate xib for the cell (again with IB) and load it using UINib upon the cells creation.

Finally, you could create the switch programmatically and add it to your cells contentview or accessoryview.

Which one suits you best largely depends on what you like to do. If your tableviews content is fixed (for a settings page etc.) the first two might work well, if the content is dynamic I'd prefer the programmatic solution. Please be more specific in what you would like to do, this would make answering your question easier.

Toastor
I'd prefer the programmatic solution (despite it's for a settings page), but I'm also interested how the first two options work. Perhaps you could explain them a little bit in more detail.
testing
A: 
if(indexPath.row == 0){//If you want UISwitch on particular row
              UISwitch *switch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
              [cell addSubview:switch];
               cell.accessoryView = switch;
        }
KiranThorat
Why do you use `initWithFrame`? Why do you use `addSubview`? `switch` can't be used as a variable name.
testing
Sorry for the switch name. I had some code.. I just changed variable name in it.
KiranThorat
+2  A: 

Setting it as the accessoryView is usually the way to go. You can set it up in tableView:cellForRowAtIndexPath: You may want to use target/action to so something when the switch is flipped. Like so:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch( [indexPath row] ) {
        case MY_SWITCH_CELL: {
            UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
            if( aCell == nil ) {
                aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease];
                aCell.textLabel.text = @"I Have A Switch";
                aCell.selectionStyle = UITableViewCellSelectionStyleNone;
                UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
                aCell.accessoryView = switchView;
                [switchView setOn:NO animated:NO];
                [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
                [switchView release];
            }
            return aCell;
        }
        break;
    }
    return nil;
}

- (void) switchChanged:(id)sender {
    UISwitch* switchControl = sender;
    NSLog( @"The switch is %@" switchControl.on ? @"ON" : @"OFF );
}
zpasternack
Instead of MY_SWITCH_CELL should be the corresponding cell number I think. Nice all around solution!
testing