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() {
    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"

  • In Swift, is it possible to convert a string to an enum?
  • Exception Types in iOS crash logs
  • UIColor code in a variable in swift
  • Reconnect a CBPeripheral faster
  • Type Any has no subscript members Error in Swift 3.0?
  • Cordova app failing to Archive with Xcode 7.1 (Cordova/CDVViewController.h file not found)
  • 2 Solutions Collect From Internet About “How to add UITableView in a UIAlertView in swift”

    I dont think you can do that with UIAlertView, but you can make a small View and present it modally or something like UIPopoverPresentationController

    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")