I know how to add any Custom UI inside UIAlertView by using accessoryView like UITableView but I am now curious that if w
Here is a Swift 5 Sample Code:
//MARK: - Properties
private var alertController = UIAlertController()
private var tblView = UITableView()
//MARK: - TableViewAlert
private func setupTableViewAlert() {
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: "Select City", message: nil, preferredStyle: .alert)
//this is the main part
//add local alert content over global one
alertController.setValue(alertVC, forKey: "contentViewController")
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}
extension SignupViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell")
cell.textLabel?.text = "Cell \(indexPath.row + 1)"
cell.textLabel?.textAlignment = .center
cell.detailTextLabel?.textColor = .black
return cell
}}
enter image description here