Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show UITableview in UIAlertcontroller in swift iOS?

I am very new to swift coding so I want to know that is there any way to show tableview in alertcontroller using swift.

like image 973
Harry Avatar asked Aug 10 '16 05:08

Harry


2 Answers

var alrController = UIAlertController(title: "\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

let margin:CGFloat = 8.0
let rect = CGRectMake(margin, margin, alrController.view.bounds.size.width - margin * 4.0, 100.0)
var tableView = UITableView(frame: rect)
tableView.delegate = self
tableView.dataSource = self
tableView.backgroundColor = UIColor.greenColor()
alrController.view.addSubview(tableView)

let somethingAction = UIAlertAction(title: "Something", style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in println("something")})

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {(alert: UIAlertAction!) in println("cancel")})

alrController.addAction(somethingAction)
alrController.addAction(cancelAction)

self.presentViewController(alrController, animated: true, completion:{})
like image 198
Mahendra Vishwakarma Avatar answered Oct 10 '22 02:10

Mahendra Vishwakarma


private var alertController = UIAlertController()
private var tblView = UITableView()
private func setupCitySelectionAlert() {
    
    let alertVC = UIViewController.init()
    let rect = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0)
    alertVC.preferredContentSize = rect.size
    
    tblView = UITableView(frame: rect)
    tblView.delegate = self;
    tblView.dataSource = self;
    tblView.tableFooterView = UIView(frame: .zero)
    tblView.separatorStyle = .singleLine
    alertVC.view.addSubview(tblView)
    alertVC.view.bringSubviewToFront(tblView)
    alertVC.view.isUserInteractionEnabled = true
    tblView.isUserInteractionEnabled = true
    tblView.allowsSelection = true
    
    self.alertController = UIAlertController(title: "Title", message: nil, preferredStyle: .alert)
    alertController.setValue(alertVC, forKey: "contentViewController")
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    
    alertController.addAction(cancelAction)
    self.present(alertController, animated: true, completion: nil)
}
like image 28
Jaipee1 Avatar answered Oct 10 '22 02:10

Jaipee1