【问题标题】:Swift use prepareForSegue in indexPathSwift 在 indexPath 中使用 prepareForSegue
【发布时间】:2015-03-05 08:54:18
【问题描述】:

我有一个模型类和两个 tableView 控制器。此类为整数值。这个类的值使用 prepareforsegue 方法发送到其他类。但我无法准备好获取 tableView 索引路径。我做错了吗?

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

    let subCategory: SubCategoryVC = segue.destinationViewController as SubCategoryVC

    let indexPath = self.categoryTableView.indexPathForSelectedRow()

    model = arrList[indexPath?.row] **//incorrect line**

    subCategory.getCatID = model.categoryID
}

【问题讨论】:

    标签: ios uitableview swift uistoryboard uistoryboardsegue


    【解决方案1】:

    indexPath?.row 返回可选值。您不能在数组 subacript 中使用它。你必须先把它包装起来。这是一个例子:

    if let indexPath = categoryTableView.indexPathForSelectedRow() {
        // do the work here
    }
    

    【讨论】:

    • 这不再是一个函数了;使用:categoryTableView.indexPathForSelectedRow
    【解决方案2】:

    是的。我找到了。这个问题是可选的。 :(

    let indexPath = self.categoryTableView.indexPathForSelectedRow()**!**
    

    我把感叹号放在行尾并改正。

    安全方法;

    if let indexPath = categoryTableView.indexPathForSelectedRow() {
        // do the work here
    }
    

    【讨论】:

    • 但是如果表达式返回 nil 就会崩溃
    猜你喜欢
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 2014-08-31
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多