问题
How do I dynamically add UITableView in UIAlertView. After adding subView of UIAlertView to UITableView it is not displayed.
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()
}
回答1:
I dont think you can do that with UIAlertView
, but you can make a small View and present it modally or something like UIPopoverPresentationController
回答2:
To create a UITableView in Swift:
Create a UITableViewController class.
Populate its delegates(no. of rows,didSelect,cellForRowAtIndexPath etc.) with necessary code.
Now call an instance of it in the
AlertViewController
.Pass needed data to the TableView (like no. of rows and array of contents).
Add the TableView instance to your alert view.
my Code Snippet:
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")
来源:https://stackoverflow.com/questions/35078186/how-to-add-uitableview-in-a-uialertview-in-swift