ios - UITableView - 什么是 indexPath?

标签 ios swift uitableview

我正在在线制作一个单元格表格教程,我有一个问题。在下面的第二个函数 tableView 中……indexPath 到底是什么?

更重要的是,程序如何知道每次调用该函数时将通过 indexPath 传递的值更新为 1?

var rowNumber = 0

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 50
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let rowCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
    rowCell.textLabel?.text = String(indexPath.row + 1)
    return rowCell
}

最佳答案

TableView 被分成几个部分。每个部分包含一定数量的行。 IndexPath 包含有关函数在哪个部分的哪一行询问的信息。基于此数字,您正在配置单元格以显示给定行的数据。应用程序在需要显示一行时执行此功能。例如,您向下滚动表格 View ,下一个单元格将出现在屏幕上。例如,要使用正确的文本配置此单元格,它会询问您应该在该位置(部分和行)的行上显示什么。

此外,为了提高性能,您应该使用 tableView.dequeueReusableCellWithIdentifier 函数而不是 Cell 的 init 创建单元格。

关于ios - UITableView - 什么是 indexPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40557686/

相关文章:

android - 获取超过 90 天的 crashlytics 报告

ios - Storyboard - UITabBarController

ios - 尝试使用 Alamofire 放置方法时,JSON 写入 (_SwiftValue) 中的类型无效

ios - 无法将类型 'UIAlertControllerStyle.Type' 的值转换为预期的参数类型 'UIAlertControllerStyle'

ios - 在 iOS9 中以编程方式打开手机设置

ios - 仅重新加载自定义 UITableViewCell 的 subview

ios - 调整按钮大小以适应任何屏幕的限制 - Xcode swift

cocoa-touch - 加载 UITableView 时崩溃

objective-c - 委托(delegate)方法 'canMoveRowAtIndexPath' 的目的是什么?

ios - swift 从领域数据库中获取所选 TableView 行的主键