LoginSignup
10
10

More than 5 years have passed since last update.

UIAlertView + UITableView

Last updated at Posted at 2012-09-25

UIAlertViewを継承したUIAlertTableView的なクラスをつかっていたら、
iOS6.0になったとき突然動かなくなってどうにも治せなかったので別の方法でやることにした。

この方法ならiOS5.1でもiOS6.0でも動くのを確認した。

// .hで以下のようなプロパティを定義
UIAlertView *alertWithTableView;

// .m
- (IBAction)showAlertWithTable:(id)sender
{
    UIAlertView *alertWithTableView = [[UIAlertView alloc] init];
    alertWithTableView.delegate = self;
    alertWithTableView.title = @"行からひとつ選択してください";
    [alertWithTableView addButtonWithTitle:@"Cancel"];
    alertWithTableView.cancelButtonIndex = 0;
    // 他のalertViewのtagと被らないように変える
    alertWithTableView.tag = 0;
    tableInAlertView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    tableInAlertView.delegate = self;
    tableInAlertView.dataSource = self;
    [alertWithTableView insertSubview:tableInAlertView atIndex:0];
    [alertWithTableView show];
}

- (void)willPresentAlertView:(UIAlertView *)alertView
{
    if (alertView.tag == 0) {
        // alertView自体のリサイズ
        CGFloat frameHeight = 250.0f;
        CGRect alertViewFrame = CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y - frameHeight/2,
                                           alertView.frame.size.width, alertView.frame.size.height + frameHeight);
        [alertView setFrame:alertViewFrame];
        // tableViewをalertView内にフィットさせるためにリサイズ
        UIView *lowestView;
        int i = 0;
        while (![[alertView.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) {
            lowestView = [alertView.subviews objectAtIndex:i];
            i++;
        }
        CGFloat tablePadding = 6.0f;
        CGFloat tableWidth = alertView.frame.size.width-(tablePadding*4);
        CGFloat tableHeight = frameHeight-(tablePadding*2);
        CGRect tableViewFrame = CGRectMake(tablePadding*2,
                                           lowestView.frame.origin.y+lowestView.frame.size.height+((tablePadding+2)*2),
                                           tableWidth, tableHeight);
        [tableInAlertView setFrame:tableViewFrame];
        tableInAlertView.layer.cornerRadius = 4;
        // 全subViewの位置をframeHeightぶん上に移動
        for (UIView *subView in alertView.subviews) {
            if ([subView isKindOfClass:[UIControl class]]) {
                subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + frameHeight,
                                           subView.frame.size.width, subView.frame.size.height);
            }
        }
    }
}

// alertViewのボタンが押されたときのデリゲート
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

}

// テーブルの行が選ばれた時のデリゲート
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 行が一つ選ばれたときにalertViewを閉じたければここで以下のように実行
    [tableInAlertView dismissWithClickedButtonIndex:0 animated:NO];
}

// 以下のテーブルの内容を決めるためのデリゲートを実装する

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10