2017-08-12 51 views
0

我的代码是这样的:如何使用默认值在alertView中创建tableView?

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

我想填充(但我不知道怎么做)的tableView这个数值在我的数组:names["name1","name2","name3"] 有人能帮助我吗?

+0

你需要添加这些值作为alertView中的选项? –

+0

请参阅此链接(https://stackoverflow.com/questions/29896005/ios-8-and-later-uitableview-inside-an-uialertcontroller) –

+3

'UIAlertController'实际上不支持添加子视图。 – rmaddy

回答

1

要填充操作表,您不要添加tableView。相反,您只需添加操作,它将私下创建和管理tableView。

对于最近的教程,请参阅UIAlertController Examples

的想法是,你的阵列中创建的每个字符串的UIAlertAction,其中包括当用户点击该操作行做什么封闭。

for name in names 
{ 
    let namedAction = UIAlertAction(title: name, style: .default) 
    { (action) in 
     // do something when this action is chosen (tapped) 
    } 
    alrController.addAction(namedAction) 
} 
+0

我只想显示名称,当我点击一个按钮之前 – Marco

+0

一个操作表是为了向用户呈现一组简单的选择。请参阅[Apple的HIG操作手册](https://developer.apple.com/ios/human-interface-guidelines/ui-views/action-sheets/)。如果你想做更复杂的事情,我建议你避免使用操作表并创建一个新的视图(和视图控制器),以便在你的应用程序中以模态方式显示。 – Smartcat

+0

或者,考虑一旦用户点击第一个操作表中的特定操作,就可以创建并显示填充了姓名的第二个操作表。 – Smartcat

0

你需要实现以填充这个数据如下tableView dataSource方法,也可以设置tableViewdatasourcealertController象下面这样: -

tableView.dataSource = alertController 

的TableView的数据源方法

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 
相关问题