Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Adding UISegmentedControls (or UISwitches, which I would personally recommend for a simple on/off option) is easy enough to put in table view cells using the accessoryView property.</p> <p>Your view controller (or whatever object you're using to control this thing) must implement the UITableViewDataSource protocol.</p> <pre><code>@interface MyViewController : UIViewController&lt;UITableViewDataSource, UITableViewDelegate&gt; {} @end </code></pre> <p>Add the controls in tableView:cellForRowAtIndexPath:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* const SwitchCellID = @"SwitchCell"; UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID]; if( aCell == nil ) { aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease]; aCell.textLabel.text = [NSString stringWithFormat:@"Option %d", [indexPath row] + 1]; aCell.selectionStyle = UITableViewCellSelectionStyleNone; UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; aCell.accessoryView = switchView; [switchView setOn:YES animated:NO]; [switchView addTarget:self action:@selector(soundSwitched:) forControlEvents:UIControlEventValueChanged]; [switchView release]; } return aCell; } </code></pre> <p>Or, if you're set on segmented controls, replace the UISwitch stuff above with something like:</p> <pre><code> UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"On", @"Off", nil]]; segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth; segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain; segmentedControl.frame = CGRectMake(75, 5, 130, 30); segmentedControl.selectedSegmentIndex = 0; [segmentedControl addTarget:self action:@selector(controlSwitched:) forControlEvents:UIControlEventValueChanged]; aCell.accessoryView = segmentedControl; [segmentedControl release]; </code></pre> <p>I would personally put these options in a regular view (probably accessible from an options button which does the flip transition, like many iPhone apps do). I don't think putting it in an alert view is a very good user experience. </p> <p>However, I have written a few blog posts showing how to put various views into alert views (<a href="http://zpasternack.blogspot.com/2010/09/uialertview-with-uitextfield.html" rel="nofollow noreferrer">text field</a>, which is kinda useful, and <a href="http://zpasternack.blogspot.com/2010/09/stupid-uialertview-tricks-part-i.html" rel="nofollow noreferrer">web view</a>, which is arguably not).</p> <p>So, if you're really dead set on putting this in an alert view, you can totally do that, like this:</p> <pre><code>- (void) doAlertWithListView { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Preferences" message:@"\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; UITableView *myView = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 150) style:UITableViewStyleGrouped] autorelease]; myView.delegate = self; myView.dataSource = self; myView.backgroundColor = [UIColor clearColor]; [alert addSubview:myView]; [alert show]; [alert release]; } </code></pre> <p>It will look something like this:</p> <p><img src="https://i.stack.imgur.com/QXrgp.png" alt="UIAlertView with a UITableView"></p>
 

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