How to add UITableView in a UIAlertView in swift

浪子不回头ぞ 提交于 2019-12-23 19:28:49

问题


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:

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


来源:https://stackoverflow.com/questions/35078186/how-to-add-uitableview-in-a-uialertview-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!