0

テーブルビューをアラートビューで表示したい。このテーブル ビューには、セグメント化されたコントロールが含まれています。これらのセグメント化されたコントロールは、オーディオ、画像、テキストのオン/オフ用です。したがって、セグメント化されたコントロールを持つ 3 つのセルがあります。

これを行う方法

私を助けてください

ありがとう

4

2 に答える 2

7

UISegmentedControls (または単純なオン/オフ オプションとして個人的にお勧めする UISwitches) の追加は、accessoryView プロパティを使用してテーブル ビュー セルに配置するのに十分簡単です。

ビュー コントローラー (またはこのことを制御するために使用しているオブジェクト) は、UITableViewDataSource プロトコルを実装する必要があります。

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

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

または、セグメント化されたコントロールに設定されている場合は、上記の 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];     

個人的には、これらのオプションを通常のビューに配置します (おそらく、多くの iPhone アプリと同様に、フリップ遷移を行うオプション ボタンからアクセスできます)。アラート ビューに配置することは、非常に優れたユーザー エクスペリエンスではないと思います。

ただし、さまざまなビューをアラート ビューに配置する方法を示すブログ投稿をいくつか書きました (テキスト フィールドはちょっと便利ですが、ウェブ ビューは間違いなくそうではありません)。

したがって、これをアラートビューに入れることに本当に固執している場合は、次のように完全に行うことができます。

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

次のようになります。

UITableView を使用した UIAlertView

于 2010-10-14T07:18:35.053 に答える
0

上記の答えはうまく機能しますが、iPad では、線myView.backgroundColor = [UIColor clearColor]; はテーブルの周りの灰色の長方形の領域を削除していないようです。私は次のことが仕事をすることを見つけます:tableView.backgroundView.alpha = 0.0;

于 2011-03-21T08:54:57.890 に答える