swift - 如何在 TableView 中使用prepareForSegue 传递数据?

标签 swift uitableview segue

我在尝试设置indexPath时遇到错误:条件绑定(bind)的初始化程序必须具有可选类型

override func prepareForSegue(segue: UIStoryboardSegue,
    sender: AnyObject?) {

        if segue.identifier == "toStation" {
            let detailViewController = segue.destinationViewController
                as! toStation
            if let indexPath = tableView.indexPathForSelectedRow! {
                detailViewController.selectedStation = stationNameArray[indexPath.row]
            }

        }
}

最佳答案

if let indexPath = tableView.indexPathForSelectedRow!

您将indexPathForSelectedRow 绑定(bind)到indexPath View ,使用if let 语句来解包可选的indexPathForSelectedRow。这是一个好方法。但同时你正在使用 bang 语句!打开可选的包装。实际上,您将可选内容展开两次。要么用!或 if let 语句。

关于swift - 如何在 TableView 中使用prepareForSegue 传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35540641/

相关文章:

json - 在 Swift 中,我如何只加载或使用来自 .json 文件的过滤数据

swift - 优先级高于闭包

ios - ObserveSingleEvent 效果不佳

uitableview - Swift:不正确地使用 UIRefreshControl - 无法识别的选择器

ios - 在使用 push segue 之前检查是否可以使用 unwind segue

swift - 如何在 UIAlertController 中应用音乐广播加载?

objective-c - UITableViewCell 上 showingDeleteConfirmation 的奇怪行为

ios - 如何返回到以前的 View Controller ?

ios - 如何使segue传递字符串起作用?

ios - UITableView 单元格 detailTextLabel 文本对齐不起作用