Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have your table structure in a NSArray, so I would suggest you to create a NSDictionary for each row that should have a segmented control and add that to the table structure NSArray</p> <p>You'll need three objects inside the dictionary. A title, a NSArray with the names of the segments, and you need a key you use to set and get the selected index.</p> <p>I did something similar for the settings viewcontroller in some of my apps. This is how the NSDictionary looks like:</p> <pre><code>[NSDictionary dictionaryWithObjectsAndKeys: @"Value of Foo", kSettingsLabel, // for the textLabel [UISegmentedControl class], kSettingsView, // which UIControl @"foo", kSettingsKey, // the key for setValue:forKey: and valueForKey: [NSArray arrayWithObjects: @"Green", @"Round", @"Auto",nil], kSettingsValue, // the titles of the segments nil] </code></pre> <p>This is how I set up the UISegmentedControls in my tableview:</p> <pre><code>NSDictionary *dictionary = [[self.dataSourceArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; // omitted: check in the dictionary which cell we need... (I wrote it that it can use almost all UIControls) // omitted: dequeue a cell with a UISegmentedControl ... // configure cell: NSArray *segmentTitles = [dictionary objectForKey:kSettingsValue]; UISegmentedControl *segment = (UISegmentedControl *)[cell viewWithTag:kTagSegment]; [segment removeAllSegments]; for (NSString *segmentName in segmentTitles) { // if index is higher than number of indexes title is inserted at last available index. // so first object in array is placed at first position in segmentcontrol [segment insertSegmentWithTitle:segmentName atIndex:1000 animated:NO]; } [segment setSelectedSegmentIndex:[[self valueForKey:[dictionary valueForKey:kSettingsKey]] intValue]]; //omitted: setup cell title ... and return cell </code></pre> <p>The UISegmentedControl is connected to a value changed action that looks like this:</p> <pre><code>NSIndexPath *indexPath = [self.settingsTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]]; if (indexPath == nil) return; NSDictionary *dictionary = [[self.dataSourceArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; if ([sender isKindOfClass:[UISegmentedControl class]]) { [self setValue:[NSNumber numberWithInt:((UISegmentedControl *)sender).selectedSegmentIndex] forKey:[dictionary valueForKey:kSettingsKey]]; } </code></pre> <p>and of course you need the setter and getter for the specified key too:</p> <pre><code>- (NSNumber *)foo { return [NSNumber numberWithInt:someValue]; } - (void)setFoo:(NSNumber *)n { someValue = [n intValue]; } </code></pre> <p>You could synthesize them but I wanted to have int value in my class instead of NSNumbers, so I wrote the setter and getter myself.</p> <hr> <p>The big advantage of this is that it is completely dynamic. If you want to rearrange cells just shift them around in the array (I use a plist to make that even easier). </p> <p>It's a little bit complicated when you use it for the first time, but it gets clear pretty fast. And you don't want five different .xibs and five different UISegmentedControls in your class interface. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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