Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add UITableView in a UIAlertView in swift

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()
}
like image 839
vignesh TN Avatar asked Dec 05 '25 05:12

vignesh TN


1 Answers

To create a UITableView in Swift:

  1. Create a UITableViewController class.

  2. Populate its delegates(no. of rows,didSelect,cellForRowAtIndexPath etc.) with necessary code.

  3. Now call an instance of it in the AlertViewController.

  4. Pass needed data to the TableView (like no. of rows and array of contents).

  5. 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")
like image 63
Amith Shaju Avatar answered Dec 07 '25 19:12

Amith Shaju