【发布时间】:2018-10-10 22:32:46
【问题描述】:
我正在学习UITableView 教程,并且在实现UITableViewDataSource 方法时对数组迭代产生了好奇。当我们调用indexPath.row 时,这是在为我们在幕后调用for 循环吗?我之所以问是因为几个月前我在学习使用来自网络服务的数据时(我已经忘记了我如何精确地做到这一点的确切步骤),但我相信我需要遍历数组以便在控制台。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Create an instance of UITableViewCell, with default appearance
let cell = UITableViewCell.init(style: .value1, reuseIdentifier: "UITableViewCell")
// Set the text on the cell with the description of the item instance that is at the nth index of the allItems array, where n = row this cell will appear in on the tableview
let item = itemStore.allItems[indexPath.row]
cell.textLabel?.text = item.name
cell.detailTextLabel?.text = "$\(item.valueInDollars)"
return cell
}
我认为indexPath.row 方法在幕后为我们迭代数组是否正确?
let item = itemStore.allItems[indexPath.row]
【问题讨论】:
-
indexPath.row只是一个数字。它本身不做任何工作......包括迭代。但是,调用者存在迭代,导致请求匹配不同行号的单元格。 -
仅供参考 - 您创建单元格的代码是错误的。您应该从表格视图中取出一个可重复使用的单元格。
标签: ios swift nsindexpath