Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could just add the <code>UISwitch</code> in your accessory view. That is the easiest way to do it, not to mention that it looks 'elegant'. </p> <p>Then, in your tableview controller code, you could just call a selector every time the switch is toggled, or even toggle the switch itself by getting the switch's current status in your controller.</p> <p>Let know if you'd like a code sample.</p> <p>---sample code---</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; //add a switch UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero]; cell.accessoryView = switchview; [switchview release]; } cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row]; return cell; } </code></pre> <p>Then, you could have a method that updates a switch based on changes in your model. You could use anything you want - delegates, notifications, KVO, etc.</p> <p>Example:</p> <pre><code>- (void)updateSwitchAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; UISwitch *switchView = (UISwitch *)cell.accessoryView; if ([switchView isOn]) { [switchView setOn:NO animated:YES]; } else { [switchView setOn:YES animated:YES]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload