【问题标题】:What happens if you don't specific an indexPath when creating a tableView Cell? cellForRowAt() [duplicate]如果在创建 tableView Cell 时没有指定 indexPath 会发生什么? cellForRowAt() [重复]
【发布时间】:2021-01-22 23:57:03
【问题描述】:

我很好奇如果您在创建 UITableViewCell 时不包含 for: indexPath 会发生什么,或者它是否不好。例如,这是从基本UITableViewController 中提取的单元格的标准创建:

let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        

我很好奇创建这样的单元格是否会产生任何负面影响:

let cellWithoutIndexPath = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

据我所知,如果您不包含 indexPath,则单元格无法知道下一步该去哪里或该做什么。 'IndexPath' 的文档描述是:

“一个索引列表,它们共同表示嵌套数组树中特定位置的路径。”

如果我有一个水果数组,我想用 [apple, orange, pear] 填充 tableView,'indexPath.row' 会指出第一个单元格应该位于数组中的位置 0 和第二个单元格应位于位置 1,等等。

另一个例子是这行代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

如果我没有包含 indexPath,tableView 将不知道我要取消选择哪个单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    相关资源
    最近更新 更多