使用动态项目获取UIAlertAction的选择索引

问题描述:

我正在构建动态动作列表。按下某个动作时,我想跳转到表格视图中的某个部分。为此,我想知道所选操作的索引,并将其替换为代码部分:“inSection:0”。任何方式做到这一点?使用动态项目获取UIAlertAction的选择索引

这里是我的代码:

let optionMenu = UIAlertController(title: nil, message: "Jump to", preferredStyle: .ActionSheet) 

for item in items { 
    let action = UIAlertAction(title: item, style: .Default, handler: { (alert: UIAlertAction!) -> Void in 
     let indexPath = NSIndexPath(forRow: 0, inSection: 0) 
     self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true) 
    }) 
    optionMenu.addAction(action) 
} 

self.presentViewController(optionMenu, animated: true, completion: nil) 

您可能需要使用以下enumerate基于循环,而不是:

for (index, item) in items.enumerate() { 
    let action = UIAlertAction(title: item, style: .Default, handler: { (alert: UIAlertAction!) -> Void in   
     let indexPath = NSIndexPath(forRow: 0, inSection: index) // <- index 
     self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true) 
    }) 
    optionMenu.addAction(action) 
} 
+0

的伟大工程。谢谢! – bashan