【发布时间】:2015-07-05 11:45:36
【问题描述】:
我希望 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 array index 0 ("iOS App Dev with Swift Essential Training","Simon Allardice")
【问题讨论】:
-
在 Swift 文档中查找“元组”。
-
Xcode 有一些很好的特性可以帮助你理解你正在查看的代码。在 Xcode 中,按住 Option 键点击上述语句行中的
row,它会告诉你它是什么。在弹出窗口中,如果您单击Reference旁边的NSIndexPath UIKit Additions,它将弹出文档。
标签: arrays swift nsindexpath