iOS-8 and later - UITableView inside an UIAlertController

不问归期 提交于 2019-11-28 10:33:24
Syed Ali Salman

Courtesy of StackOverflow users I was able to do this task.

Here is my code:

UIViewController *controller = [[UIViewController alloc]init];
UITableView *alertTableView;
CGRect rect;
if (array.count < 4) {
    rect = CGRectMake(0, 0, 272, 100);
    [controller setPreferredContentSize:rect.size];

}
else if (array.count < 6){
    rect = CGRectMake(0, 0, 272, 150);
    [controller setPreferredContentSize:rect.size];
}
else if (array.count < 8){
    rect = CGRectMake(0, 0, 272, 200);
    [controller setPreferredContentSize:rect.size];

}
else {
    rect = CGRectMake(0, 0, 272, 250);
    [controller setPreferredContentSize:rect.size];
 }

alertTableView  = [[UITableView alloc]initWithFrame:rect];
alertTableView.delegate = self;
alertTableView.dataSource = self;
alertTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
[alertTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
[alertTableView setTag:kAlertTableViewTag];
[controller.view addSubview:alertTableView];
[controller.view bringSubviewToFront:alertTableView];
[controller.view setUserInteractionEnabled:YES];
[alertTableView setUserInteractionEnabled:YES];
[alertTableView setAllowsSelection:YES];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];
[alertController setValue:controller forKey:@"contentViewController"];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

}];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];

meaning-matters

Here's @Syed Ali Salman's answer in simplified form in Swift:

let alertController = UIAlertController(title: "The Title",
                                        message: "Here's a message.",
                                        preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel)
{ (action) in
    // ...
}
alertController.addAction(cancelAction)

let okAction = UIAlertAction(title: "OK", style: .Default)
{ (action) in
    // ...
}
alertController.addAction(okAction)

let tableViewController = UITableViewController()
tableViewController.preferredContentSize = CGSize(width: 272, height: 176) // 4 default cell heights.
alertController.setValue(tableViewController, forKey: "contentViewController")

yourTopViewController().presentViewController(alertController, animated: true)
{
    // ...
}
M David
UIViewController *tempViewController = [[UIViewController alloc] init];
    tempViewController.view.backgroundColor = [UIColor redColor];

    [alertController setValue:tempViewController forKey:@"contentViewController"];

That piece of code will show a red view on the alert view,Now you can easily put a UITableView inside the UIViewController.Happy UIAlertController customizing ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!