【问题标题】:What is indexPath in NSIndexPath什么是 NSIndexPath 中的 indexPath
【发布时间】:2015-11-24 15:06:14
【问题描述】:

我有这堆代码

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel?.text = toDoList[indexPath.row] //will give us the 0th, 1st, 2nd etc..

    return cell

有人可以向我解释第一行中的“indexPath”是什么吗?

【问题讨论】:

标签: swift tableview nsindexpath


【解决方案1】:

indexPath外部名称为cellForRowAtIndexPath的参数的本地名称。

indexPath 是在函数体内部定义的(你确实使用它),而函数调用者在命名函数参数时必须使用cellForRowAtIndexPathlet cell = tableView.delegate.tableView(tableView, cellForRowAtIndexPath: ...)

比较:

func sum1(a: Int, b: Int) -> Int {
    return a + b
}

func sum2(a: Int, _ b: Int) -> Int {
    return a + b
}

func sum3(integer a: Int, secondInteger b:Int) -> Int {
    return a + b
}

sum1(1, b: 2)
sum2(1, 2)
sum3(integer: 1, secondInteger: 2)

是时候查看有关函数参数名称的 Apple 文档了:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      NSIndexPathNSObject 的子类。它表示数组集合树中特定节点的路径。

      it has clearly mentioned in here. read this

      您的问题基于 tableView。您在表格视图中有一组单元格。所以节点是您选择的单元格。

      indexPath 代表您在此处的树中选择的单元格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多