如何在swift中在UIAlertView中添加UITableView
如何在UIAlertView中dynamic添加UITableView。 将UIAlertView的子视图添加到UITableView后,不显示。
override func viewDidLoad() { super.viewDidLoad() tableView.frame = CGRectMake(0, 50, 320, 200); tableView.delegate = self tableView.dataSource = self tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") } @IBAction func textFieldCliked(sender: AnyObject) { alertView.message = "table view" alertView.addButtonWithTitle("Ok") alertView.addSubview(tableView) alertView.show() }
我不认为你可以用UIAlertView
做到这一点,但你可以做一个小的视图,模式地展示它,或者像UIPopoverPresentationController
在Swift中创build一个UITableView:
-
创build一个UITableViewController类。
-
使用必要的代码填充它的委托(行号,didSelect,cellForRowAtIndexPath等)。
-
现在在
AlertViewController
调用它的一个实例。 -
将所需的数据传递给TableView(如行号和内容数组)。
-
将TableView实例添加到您的警报视图。
我的代码片段:
let alertController : UIAlertController = UIAlertController(title: "Select email ID", message: nil, preferredStyle: .Alert) alertController.view.backgroundColor = UIColor.whiteColor() alertController.view.layer.cornerRadius = 8.0 let contactNumVC = contactNumberTableViewController () contactNumVC.count = emailIDs.count contactNumVC.numbersArray = emailIDs contactNumVC.preferredContentSize = CGSizeMake(alertController.view.frame.width, (44 * CGFloat(emailIDs.count))) alertController.setValue(contactNumVC, forKeyPath: "contentViewController")