【问题标题】:Table View Cell Description表格视图单元格说明
【发布时间】: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


    【解决方案1】:

    IndexPath 上的Apple's documentation 表示:索引路径中的每个索引都表示从树中的一个节点到另一个更深节点的子数组的索引。

    简单地说,它基本上意味着IndexPaths 是一种访问二维数组的方法,这就是tableView 的dataSource 的含义。 tableView 需要知道它有多少个部分,以及每个部分有多少行。

    在您的情况下,只有一个部分,因此您无需担心 indexPath.section,因为该部分始终为 0。tableView 的多维中只有一个数组(您的 cellContent 数组) dataSource 以便您可以使用indexPath.row 访问元素。如果您有多个 cellsContent 数组,则必须先使用 indexPath.section 才能访问正确的数组,然后才能使用 indexPath.row

    您已经省略了 UITableViewDatasourcenumberOfSections 方法,该方法默认返回 1

    【讨论】:

      【解决方案2】:

      除了 Tom Pearson 对 IndexPath 的回答之外,

      internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
      

      是的,对于 tableView 中的每个可见单元格都会调用此方法。

      如果您在 cellForRowAt 方法中使用以下方法,则单元格将被重用,而无需实例化更多单元格对象。

      let cell = tableView.dequeueReusableCell(withIdentifier: action,
                                                         for: indexPath as IndexPath)
      

      一旦单元格不可见(假设它被滚动),单元格对象将被重新用于新可见的行,而不是为每个单元格新创建。这就是这种方法的强大之处。

      通常代码会是这样的。

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
          guard let cell = tableView.dequeueReusableCell(withIdentifier: "someIdentifier",
                                                         for: indexPath as IndexPath) as? SomeCell else {
              fatalError("Cell is not of type SomeCell")
          }
      
          cell.title.font = somefont
          cell.title?.textColor = somecolor
          cell.backgroundColor = UIColor.clear
      
          return cell
      }
      

      【讨论】:

      • @Lara senchina,这有帮助吗?你的问题解决了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多