【发布时间】:2019-03-23 20:38:14
【问题描述】:
我正在通过在线课程学习 iOS。我正在使用 Swift 4.2。
我的问题是关于这种方法的:
// This function is defining each cell and adding contenet to it.
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
上面的方法在下面的代码中究竟是如何工作的?我知道上面的方法描述了表格视图的每个单元格,但是表格视图是否会为每一行调用它?
indexpath.row 到底是什么意思?我对这个感到困惑。
请帮助我。谢谢。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var cellContent:Array<String> = ["Amir", "Akbar", "Anthony"]
// This function is setting the total number of cells in the table view
internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
// This function is defining each cell and adding contenet to it.
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
【问题讨论】:
标签: ios swift uitableview