3

我正在建立一个动态的动作列表。当按下一个动作时,我想跳到表格视图中的一个部分。为此,我想知道操作的选定索引并将其替换为代码部分:“inSection:0”。有什么办法吗?

这是我的代码:

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)
4

1 回答 1

7

您可能希望改用以下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)
}
于 2016-01-23T15:41:16.833 回答