我可以在每个单元格中使用带有分段控件的表格视图显示警报视图

roc*_*key 0 iphone uitableview uisegmentedcontrol uialertview

我想在警报视图中显示表视图.此表视图包含分段控件.这些分段控件用于打开/关闭音频,图像,文本.因此将有3个细胞具有分段对照.

这该怎么做

请帮帮我

感谢你

zpa*_*ack 7

添加UISegmentedControls(或UISwitches,我个人推荐用于简单的开/关选项)很容易使用accessoryView属性放入表视图单元格.

您的视图控制器(或您用来控制此事物的任何对象)必须实现UITableViewDataSource协议.

@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {}
@end
Run Code Online (Sandbox Code Playgroud)

在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;
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您设置了分段控件,请使用以下内容替换上面的UISwitch:

    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];     
Run Code Online (Sandbox Code Playgroud)

我个人会把这些选项放在一个常规视图中(可能从一个选项按钮可以访问,它可以像许多iPhone应用程序那样进行翻转过渡).我不认为把它放在警报视图中是一种非常好的用户体验.

但是,我写了一些博客文章,展示如何将各种视图放入警报视图(文本字段,这是有用的,和Web视图,可以说是没有).

所以,如果你真的把它放在一个警报视图中,你可以完全这样做,像这样:

- (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];    
}
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

UIAlertView带有UITableView