Swift 中的 NSIndexPath

NSIndexPath in Swift

我希望 class NSIndexPath 处理具有数组的数组。

我有这个代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource {

    let devCourses = [
        ("iOS App Dev with Swift Essential Training","Simon Allardice"),
        ("iOS 8 SDK New Features","Lee Brimelow"),
        ("Data Visualization with D3.js","Ray Villalobos"),
        ("Swift Essential Training","Simon Allardice"),
        ("Up and Running with AngularJS","Ray Villalobos"),
        ("MySQL Essential Training","Bill Weinman"),
        ("Building Adaptive Android Apps with Fragments","David Gassner"),
        ("Advanced Unity 3D Game Programming","Michael House"),
        ("Up and Running with Ubuntu Desktop Linux","Scott Simpson"),
        ("Up and Running with C","Dan Gookin") ]

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return devCourses.count
    }

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

        var cell = UITableViewCell()

        let (courseTitle, courseAuthor) = devCourses[indexPath.row]
        cell.textLabel?.text = courseTitle

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

我不明白 NSIndexPath 是如何工作的。我已经阅读了文档,但在我学习它是如何工作的这一点上,我仍然很难理解它是如何工作的。我怎么知道 NSIndexPath 有 属性 行?请有人解释一下这一行的每一部分:

let (courseTitle, courseAuthor) = devCourses[indexPath.row]

NSIndexPath 如何知道这个常量中的 courseTitle 引用索引号 0 whitin 数组索引 0 ("iOS App Dev with Swift Essential Training","Simon Allardice")

稍微偏离主题,但我想鼓励大家为模型使用自定义 classes 而不是 "primitive" 类型。事半功倍,收获大。

简单 class 具有两个属性和一个描述(描述变量在调试时很有帮助)

class DevCourse : Printable {

  var author : String
  var title : String

  init(author : String, title : String) {
    self.author = author
    self.title = title
  }

  var description : String { return "DevCourse \(title) by \(author)"}
}

devCourses 数组可以很容易地映射到用一行创建 DevCourse 个实例

  let rawDevCourses = [
    ("iOS App Dev with Swift Essential Training","Simon Allardice"),
    ("iOS 8 SDK New Features","Lee Brimelow"),
    ("Data Visualization with D3.js","Ray Villalobos"),
    ("Swift Essential Training","Simon Allardice"),
    ("Up and Running with AngularJS","Ray Villalobos"),
    ("MySQL Essential Training","Bill Weinman"),
    ("Building Adaptive Android Apps with Fragments","David Gassner"),
    ("Advanced Unity 3D Game Programming","Michael House"),
    ("Up and Running with Ubuntu Desktop Linux","Scott Simpson"),
    ("Up and Running with C","Dan Gookin") ]

let devCourses = rawDevCourses.map { DevCourse(author: [=11=].1, title: [=11=].0) }

应用属性的线条看起来更清晰

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

    let cell = tableView.dequeueReusableCellWithIdentifier("MyIdentifier", forIndexPath: indexPath) as! UITableViewCell

    let course = devCourses[indexPath.row]
    cell.textLabel?.text = course.title

    return cell
}

What I don't understand how NSIndexPath works.

对于 iOS,您可以将 NSIndexPath 视为包含两个 Int 属性 sectionrow 的只读结构,如果您如果您正在使用 UICollectionViews,则重新使用 UITableViews 或 sectionitem

您使用 NSIndexPath:forRow:inSection: 工厂方法创建它们:

let indexPath = NSIndexPath(forRow: 1, inSection: 0)

并通过访问它们的属性来阅读它们:

print("row is \(indexPath.row)")

How can I know NSIndexPath has property of row?

Xcode 有一些很好的功能可以帮助您理解您正在查看的代码。在Xcode、选项——点击上面语句行中的row,它会告诉你是什么。在弹出窗口中,如果您单击 Reference 旁边的 NSIndexPath UIKit Additions,它将调出文档。

Can somebody explain every part of this line please:

let (courseTitle, courseAuthor) = devCourses[indexPath.row]

devCourses 是一个包含两个 String 类型值的元组数组。您可以通过选项-单击devCourses看到它,它显示[(String, String)]。如果它是 String 的数组,它会说 [[String]][Array<String>] (这是表达同一事物的两种方式)。

indexPath.row 只是一个 Int 表示 tableView.

的选定行

devCourses[indexPath.row] 然后是该索引处的元组。例如,如果 row0,则元组将是 ("iOS App Dev with Swift Essential Training","Simon Allardice").

最后,您可以通过将两个变量声明为元组并使用元组对其进行初始化来同时初始化两个变量。例如:

let (a, b) = (3, 4)

创建两个 Int 常量 ab 并将 3 分配给 a 并将 4 分配给 b

所以这条语句是从整数 indexPath.row 指示的数组中检索元组并创建两个变量,将元组中第一个值的值赋给第一个变量 courseTitle 并将第二个变量 courseAuthor 元组中第二个值的值。


更新 Swift 3

NSIndexPath 仍然存在于 Foundation 中,但是当在 Swift 3 中使用 indexPaths 时,一个新的现在使用 IndexPath 类型。此类型是 结构 .

您仍然可以在 Swift 3 中创建一个 NSIndexPath,使用以下更新的语法,但您不能更改属性:

var ip = NSIndexPath(row: 0, section: 0)

ip.row = 5    // Cannot assign to property: 'row' is a get-only property

但您应该改用新的 IndexPath 结构。

IndexPath 使用类似的语法创建:

var ip2 = IndexPath(row: 0, section: 0)

ip2.row = 5   // this works

您可以在 IndexPathNSIndexPath 之间转换,方法是使用 as:

let ip = NSIndexPath(row: 0, section: 0)
let ip2 = ip as IndexPath     // convert to IndexPath
let ip3 = ip2 as NSIndexPath  // convert back to NSIndexPath

所有 CocoaCocoa Touch API 使用 indexPaths已转换为在 Swift.

中使用 IndexPath