3

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()
}
sKhan
  • 9,694
  • 16
  • 55
  • 53
vignesh TN
  • 383
  • 4
  • 7
  • I have not added let alertView = UIAlertView() here..but i have added in my code..still its not working – vignesh TN Jan 29 '16 at 06:28
  • You can't do this.Just for in your items and add buttons – Lumialxk Jan 29 '16 at 06:32
  • 3
    Firstly, `UIAlertView` is deprecated as of iOS 8, so you probably should take a look at [UIAlertController](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/). Secondly, Alerts aren't designed to contain custom subviews (like a TableView). Consider presenting your table in a modal as Alerts are designed to allow users to make a selection from some available actions. – tebs1200 Jan 29 '16 at 06:36

3 Answers3

3

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")
Artem Mostyaev
  • 3,874
  • 10
  • 53
  • 60
Amith Shaju
  • 143
  • 7
0

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

Encio Peter
  • 329
  • 4
  • 13
0

To use alert type behaviour with table/image or custom button actions in several places of app, I have created a sample project Generic popover with all these examples. This is not using transition delegate. This project also demo custom font and color to be adjusted to your app Theme.

create a custom tableview/imageview/buttons in storyboard and call as following

if let vc = self.storyboard?.instantiateViewController(withIdentifier: "TableContentViewController") as? TableContentViewController{
            vc.modalPresentationStyle = .overCurrentContext
             vc.popOverDelegate = self
            self.present(vc, animated: false, completion: nil)
        }

More information in the link https://github.com/shruezee/GenericPopovers

Shruthi Pal
  • 89
  • 1
  • 3