views:

34

answers:

1

Hi all

i want to display the table view in alert view. This table view contains segmented control. these segmented controls are for on/off the audio, image, text. so there will be 3 cells with segmented controls.

how to do this

please help me out

Thank u

+2  A: 

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.

Your view controller (or whatever object you're using to control this thing) must implement the UITableViewDataSource protocol.

@interface MyViewController : UIViewController<UITableViewDataSource> {}
@end

Add the controls in tableView:cellForRowAtIndexPath:

- (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;
}

Or, if you're set on segmented controls, replace the UISwitch stuff above with something like:

    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];     

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.

However, I have written a few blog posts showing how to put various views into alert views (text field, which is kinda useful, and web view, which is arguably not).

So, if you're really dead set on putting this in an alert view, you can totally do that, like this:

- (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];    
}

It will look something like this:

UIAlertView with a UITableView

zpasternack
Thanks pasternack !! i will try it...and let u know. Thanks again...!!
rockey
Hi Pasternack ! its worked...!! Thank you..:]
rockey