首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS-8及更高版本- UITableView在UIAlertController中

iOS-8及更高版本- UITableView在UIAlertController中
EN

Stack Overflow用户
提问于 2015-04-27 12:36:53
回答 4查看 11.3K关注 0票数 5

我知道如何通过使用类似于Custom UIaccessoryViewUIAlertView中添加任何Custom UI,但我现在很好奇,如果我们仍然可以选择在UIAlertController中添加Custom UI,那么我想要的是在UIAlertController中添加一个具有清晰理解的UITableViewController

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-04-28 04:18:51

多亏了StackOverflow用户,我才能完成这个任务。

这是我的代码:

代码语言:javascript
复制
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];

票数 27
EN

Stack Overflow用户

发布于 2016-08-31 15:13:22

以下是Swift中简化形式的@Syed Ali Salman的答复

代码语言:javascript
复制
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)
{
    // ...
}
票数 9
EN

Stack Overflow用户

发布于 2015-04-27 12:45:17

代码语言:javascript
复制
UIViewController *tempViewController = [[UIViewController alloc] init];
    tempViewController.view.backgroundColor = [UIColor redColor];

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

这段代码将在警报视图上显示一个红色视图,现在您可以轻松地将一个UITableView放在UIViewController.Happy UIAlertController自定义中;)

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29896005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档