Due to the ubiquitousness of using UITableViews to display various types of UIs in iOS, many different types of content end up inside UITableViewCells. One common approach involves putting a
UISwitch inside a table cell to represent an on/off setting:
So how do you actually go about making this type of cell? The method I’m going to describe here doesn’t require a custom UITableViewCell subclass so that makes it easy to use in just about any iOS project.
Configuring a cell to display a UISwitch
The first step is to configure your table cell to contain a
UISwitch and a
UILabel (for the description):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
This looks like a lot of code, but most of it is related to positioning the label and the switch in the table cell.
First, the switch. On line 7, we get the size needed to display the switch. Then the switch’s frame can be calculated (lines 8 - 11). The
x position of the switch is calculated by subtracting the switch’s width (plus a little padding) from the cell’s
contentView width. This places the switch against the right-hand side of the cell (inset by the padding amount). The
y position of the switch is calculated to center the switch vertically in the cell.
The label is a little simpler. On line 19
CGRectInset is used to give us a
CGRect with a horizontal padding of 10 and a vertical padding of 8. Then the width of the label is shrunk on line 20 to be half the width of the cell.
Once the cell is configured, we set the switch’s value (finding it via the tag assigned on line 13) and we’re off to the races.
Responding to the switch being switched
Now we have a cell that displays a label and a switch with the switch’s value set appropriately. How do we handle the user tapping the switch? On line 14 in the setup code above, we added an action to be called when the switch’s value is changed. We need to implement that action method:
1 2 3 4 5
Rounding out the behavior
There’s one additional step we can take to improve the cell’s behavior. Right now, if the user taps anywhere in the cell that isn’t the switch, nothing happens. We should make it so that tapping the cell anywhere (the label, the padding, etc.) triggers the switch. That can be handled in the table view’s delegate:
1 2 3 4 5 6 7
First we get a reference to the cell that was tapped, then the switch in that cell. Line 5 animates toggling of the switch’s value. Then we call the same action method that was assigned to the switch to process the value.
There are other ways to include a
UISwitch in a
UITableViewCell. One method is to subclass
UITableViewCell and implement similar logic to that shown here. That method is useful for encapsulating the switch and label related code. If you take that route, you will probably want to create a delegate for your new subclass to allow the consuming code to be notified of switch value changes.