如何在Swift iOS中使用UIAlertController显示UITableview?

6

我对Swift编程非常陌生,想知道是否有办法在Swift中使用AlertController显示TableView。


你的需求是什么? - Pandey_Laxman
请查看此链接:https://dev59.com/_3rZa4cB1Zd3GeqP345A - Anbu.Karthik
谢谢,我已经能够展示表格视图了,但是如何在表格视图单元格点击时取消同一个UIAlertController呢? - Harry
2个回答

5
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:{})

如果您能简要解释一下您的代码是做什么的,那就可以节省一些时间。谢谢 :) - iPeter

2
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)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接