UISwitch in a UITableView cell

IphoneObjective CCocoa TouchUitableviewUiswitch

Iphone Problem Overview


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;

Iphone Solutions


Solution 1 - Iphone

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 do 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" );
}

Solution 2 - Iphone

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.

Solution 3 - Iphone

if (indexPath.row == 0) {//If you want UISwitch on particular row
    UISwitch *theSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
    [cell addSubview:theSwitch];
    cell.accessoryView = theSwitch;
}

Solution 4 - Iphone

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.

Solution 5 - Iphone

for swift users

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "TableIdentifer")
        let aswitch = UISwitch()
        cell.accessoryView = aswitch 
}

Solution 6 - Iphone

This is a more complete solution where turning off and on happens on the view layer (UITableViewCell) and it forwards the events to the tableView delegate through didSelect and didDeselect:

class CustomCell: UITableViewCell {
    private lazy var switchControl: UISwitch = {
        let s = UISwitch()
        s.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
        return s
    }()
    
    override func awakeFromNib() {
        self.accessoryView = switchControl
        self.selectionStyle = .none // to show the selection style only on the UISwitch
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        (self.accessoryView as? UISwitch)?.isOn = selected
    }
    
    @objc private func switchValueDidChange(_ sender: UISwitch) { // needed to treat switch changes as if the cell was selected/unselected
        guard let tv = self.superview as? UITableView, let ip = tv.indexPath(for: self) else {
            fatalError("Unable to cast self.superview as UITableView or get indexPath")
        }
        setSelected(sender.isOn, animated: true)
        if sender.isOn {
            tv.delegate?.tableView?(tv, didSelectRowAt: ip)
        } else {
            tv.delegate?.tableView?(tv, didDeselectRowAt: ip)
        }
    }
}

And on your delegate


func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
    return false // to disable interaction since it happens on the switch
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // to make sure it is rendered correctly when dequeuing:
    // stuff
    if isSelected { // stored value to know if the switch is on or off
        tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
    } else {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    // more stuff
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // do your thing when selecting
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    // do your thing when deselecting
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontestingView Question on Stackoverflow
Solution 1 - IphonezpasternackView Answer on Stackoverflow
Solution 2 - IphoneEchelonView Answer on Stackoverflow
Solution 3 - Iphonek-thoratView Answer on Stackoverflow
Solution 4 - IphoneToastorView Answer on Stackoverflow
Solution 5 - Iphoneknig_TView Answer on Stackoverflow
Solution 6 - IphonergkobashiView Answer on Stackoverflow