objective-c - 如何在 iOS8 的 UIAlertController 中添加 UITableview?

标签 objective-c uitableview ios8 uialertcontroller

直到 iOS7 在自定义 View 中,我们可以按照下图将 tableview 设置为 alert。

enter image description here

但在 iOS8 中 UITableview 不工作。我可以把它放在 tableview 中,但 tableview 没有滚动,也没有检测到对任何单元格的任何点击。

enter image description here

那么,我该如何实现这种类型的 AlertController 呢?

最佳答案

礼貌 StackOverFlow 用户我能够完成这项任务...... 这是我的代码...

 UIViewController *controller = [[UIViewController alloc]init];
UITableView *alertTableView;
if (array.count < 4) {
    CGRect rect = CGRectMake(0, 0, 272, 100);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 100)];
}
else if (array.count < 6){
    CGRect rect = CGRectMake(0, 0, 272, 150);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 150)];
}
else if (array.count < 8){
    CGRect rect = CGRectMake(0, 0, 272, 200);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 200)];
}
else{
    CGRect rect = CGRectMake(0, 0, 272, 250);
    [controller setPreferredContentSize:rect.size];
    alertTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 272, 250)];
}

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

Checkout the snapshot here ;)

关于objective-c - 如何在 iOS8 的 UIAlertController 中添加 UITableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635814/

相关文章:

iphone - 将 NSFetchedResultsController 从 UITableViewController 迁移到 UIViewController

ios - 如何使用 Stackview 消费整个 View

ios - UIStackView 未出现在容器 View 中

ios - 初始加载时导航 Controller 后面的 UITableViewController 中的 UITableView

iOS8 区域本地化(例如 pt-BR)?

objective-c - Interface Builder 有没有办法渲染不覆盖 drawRect : 的 IBDesignable View

objective-c - 通过代码将标题/图标等设置为在 IB 中创建的 TabBarItem?

ios - 在hackintosh上开发,运行模拟器iOS应用程序

iOS 日历事件 View 表示

objective-c - 代码 : How to handle Space character in string?