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()
}
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With