简体   繁体   English

表格视图单元格说明

[英]Table View Cell Description

Im following a course online to learn iOS. 我正在网上学习iOS。 Im using Swift 4.2. 我正在使用Swift 4.2。

My question is about this method: 我的问题是关于这种方法的:

// 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

    }

Exactly how the above method works in the following code? 上面的方法在下面的代码中究竟是如何工作的? I know that the above method describes each cell of the Table View but does it gets called by the table view for each row? 我知道上面的方法描述了表格视图的每个单元格,但是表格视图是否为每一行调用了它?

What does indexpath.row means exactly? indexpath.row到底是什么意思? Im confused with this one. 我对此感到困惑。

Please help me. 请帮我。 Thanks. 谢谢。

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.
    }


}

Apple's documentation on IndexPath says: Each index in an index path represents the index into an array of children from one node in the tree to another, deeper, node. 苹果公司在IndexPath上的文档说:索引路径中的每个索引代表从树中一个节点到另一个更深节点的子级数组的索引。

In plain English it basically means IndexPath s are a way to access a two dimensional array, which is what the dataSource of a tableView is. 用通俗的英语来说,它基本上意味着IndexPath是访问二维数组的一种方法,而tableView的dataSource就是这种二维数组。 The tableView needs to know how many sections it has, and how many rows there are in each section. tableView需要知道它有多少节,以及每个节中有多少行。

In your case there is only one section so you don't need to worry about indexPath.section because the section is always 0. There is only one array (your cellContent array) in the tableView 's multidimensional dataSource so you can access elements using indexPath.row . 在你的情况下,只有一个部分,所以你不必担心indexPath.section因为部分始终为0,只有一个数组(您cellContent阵列)在tableView的多维数据源,所以你可以使用访问元素indexPath.row If you had more than one cellsContent Array you would have to use indexPath.section to access the right one before you could use indexPath.row 如果您有不止一个cellsContent Array,则必须先使用indexPath.section来访问正确的一个,然后才能使用indexPath.row

You've omitted the numberOfSections method of UITableViewDatasource which returns 1 by default. 您已经省略了UITableViewDatasourcenumberOfSections方法,该方法默认情况下返回1

In addition to Tom Pearson answer on IndexPath, 除了汤姆·皮尔森(Tom Pearson)在IndexPath上的答案外,

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

Yes, this method is called for every visible cells in the tableView. 是的,为tableView中的每个可见单元格调用此方法。

If you use below method in cellForRowAt method, cells will be reused without instantiating more cell objects. 如果在cellForRowAt方法中使用下面的方法,则将在不实例化更多单元对象的情况下重用单元。

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

Once the cell goes out of visibility (lets say it is scrolled), the cell object is reused for newly visible row and it is not newly created for each and every cells. 一旦该单元格失去可见性(可以滚动),该单元格对象将重新用于新的可见行,并且不会为每个单元格新创建该对象。 There lies the power of this method. 这就是这种方法的强大之处。

Usually code will be like this. 通常,代码将是这样的。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM