0

My code is this:

let alrController = UIAlertController(title: "Membri", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
tableView.backgroundColor = UIColor.white
alrController.view.addSubview(tableView)
let cancelAction = UIAlertAction(title: "Esci", style: UIAlertActionStyle.cancel, handler: {(alert: UIAlertAction!) in})
alrController.addAction(cancelAction)
self.present(alrController, animated: true, completion:{})

I want to populate (but I don't know how) the tableView with this values in my array: names["name1","name2","name3"] Can someone help me?

Marco
  • 65
  • 11
  • You need to add those values as options in alertView? – Reinier Melian Aug 12 '17 at 16:56
  • Please see this [link](https://stackoverflow.com/questions/29896005/ios-8-and-later-uitableview-inside-an-uialertcontroller) – Aditya Srivastava Aug 12 '17 at 16:58
  • 3
    `UIAlertController` doesn't actually support adding subviews. – rmaddy Aug 12 '17 at 16:58
  • This code is right and shows a tableView in the alert. But I can't populate the tableView with values – Marco Aug 12 '17 at 17:05
  • @Marco Please follow the above link, you may get some help to solve this. – Aditya Srivastava Aug 12 '17 at 17:10
  • 1
    @Marco From the documentation for `UIAlertController`: *"The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified."*. Adding subviews is not supported. You may get it to work but that doesn't mean it is supported. And what works now may break in the next iOS update. – rmaddy Aug 12 '17 at 17:21

3 Answers3

1

To populate an action sheet, you don't add a tableView. Instead, you simply add the actions and it will create and manage the tableView privately.

For a recent tutorial, see UIAlertController Examples

The idea is that you'd create an UIAlertAction for each String in your array, including a closure for what to do when user taps that action row.

for name in names
{
    let namedAction = UIAlertAction(title: name, style: .default) 
    { (action) in
        // do something when this action is chosen (tapped)
    }
    alrController.addAction(namedAction)
}
Smartcat
  • 2,834
  • 1
  • 13
  • 25
  • I only want to show the names, when I click on a button before – Marco Aug 12 '17 at 17:14
  • An action sheet is meant to be a simple set of choices to present to the user. See [Apple's HIG on action sheets](https://developer.apple.com/ios/human-interface-guidelines/ui-views/action-sheets/). If you want to do something more complex, I advise you avoid the action sheet and create a new view (and view controller) to be displayed modally in your app. – Smartcat Aug 12 '17 at 17:19
  • Alternatively, consider creating and displaying a second action sheet populated with your names, once the user taps a specific action in the first action sheet. – Smartcat Aug 12 '17 at 17:29
0

You need to implement the following tableView dataSource methods in order to populate this data and also set tableView datasource to alertController like below:-

tableView.dataSource = alertController

TableView DataSource method

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names[section].count
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell: yourCustomCell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as? yourCustomCell 
else {
  fatalError("yourCustomCell not found")
}
cell.textLabel.text = names[indexpath.row]
return cell
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
0

You can use custom UIViewController transition to achieve this kind of functionality.

This link: https://github.com/pgpt10/Custom-Animator will give you an idea of how you can achieve that.

PGDev
  • 23,751
  • 6
  • 34
  • 88