【问题标题】:Does indexpath.row take care of iterating an array?indexpath.row 是否负责迭代数组?
【发布时间】:2018-10-10 22:32:46
【问题描述】:

我正在学习UITableView 教程,并且在实现UITableViewDataSource 方法时对数组迭代产生了好奇。当我们调用indexPath.row 时,这是在为我们在幕后调用for 循环吗?我之所以问是因为几个月前我在学习使用来自网络服务的数据时(我已经忘记了我如何精确地做到这一点的确切步骤),但我相信我需要遍历数组以便在控制台。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Create an instance of UITableViewCell, with default appearance
        let cell = UITableViewCell.init(style: .value1, reuseIdentifier: "UITableViewCell")

        // Set the text on the cell with the description of the item instance that is at the nth index of the allItems array, where n = row this cell will appear in on the tableview
        let item = itemStore.allItems[indexPath.row]

        cell.textLabel?.text = item.name
        cell.detailTextLabel?.text = "$\(item.valueInDollars)"

        return cell
    }

我认为indexPath.row 方法在幕后为我们迭代数组是否正确?

let item = itemStore.allItems[indexPath.row]

【问题讨论】:

  • indexPath.row 只是一个数字。它本身不做任何工作......包括迭代。但是,调用者存在迭代,导致请求匹配不同行号的单元格。
  • 仅供参考 - 您创建单元格的代码是错误的。您应该从表格视图中取出一个可重复使用的单元格。

标签: ios swift nsindexpath


【解决方案1】:

不,调用indexPath.row 不会为您遍历所有行。它只是一个发送到cellForRowAt 的变量,您可以访问并使用它来创建您的单元格。它包含有关调用函数 cellForRowAt 的哪一行的信息。

然而,cellForRowAt 实际上会在您的 tableVIew 上每次显示单元格时调用。假设您有一个包含 100 个项目的 dataSource,一次只能看到 10 个单元格。当 tableView 初始加载时,cellForRowAt 将被调用 10 次。然后,如果您滚动 tableView 以再显示 3 个单元格,cellForRowAt 将再被调用 3 次。

【讨论】:

    【解决方案2】:

    首先,教程似乎很糟糕。表格视图单元格应该被重用

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

    显示表格视图单元格的工作流程是:

    • 框架调用numberOfRowsInSection(以及numberOfSections
    • 对于可见单元格范围内的每个节/行对,它调用cellForRowAt 并在第二个参数中传递索引路径。

    【讨论】:

      【解决方案3】:

      没有。 indexPath 只是一个带有sectionrow 的结构。您可以使用它们直接从阵列中查找信息。没有迭代发生。

      cellForRowAt 仅对屏幕上的单元格调用,因此调用顺序取决于您滚动的方向。

      事实上,UITableView 甚至不知道您是使用数组还是动态生成信息。你对indexPathrowsection做什么取决于你。

      【讨论】:

        【解决方案4】:

        查看 UITableView 和 cellForRowAtIndexpath 函数的文档。

        在 tableView 中定位一行的索引路径。

        IndexPath 将是一个结构,它可以帮助您从嵌套数组中获取特定位置。在您的表格视图的情况下,该部分内的行。

        cellForRow 将返回特定索引路径的单元格(在指定的部分和行中)

        因此 indexPath.section 将给出单元格将被修改并返回到数据源的节号。并且在节内,indexPath.row 将是该节内的对应行。

        index path documentation

        tableView-CellForRowAt: IndexPath

        UITableView DataSource Documentation

        [更新]

        如果要添加多个部分,请添加另一个数据源numberOfSections,并返回从中计数的部分数量。

        我可以链接到来自 Apple 的 create a table view tutorial,但这是一个很长的文档,您可以跳过开头并阅读在表格视图中显示部分

        如果需要,您可以使用 2D 数组来保存部分和行。在numberOfSections方法中返回array.count,在numberOfRows:inSection中返回array[section].count

        更多示例,

        谢谢, J

        【讨论】:

        • 根据你的回答,我可以问一下,为了有一个包含两个部分的 tableView,我需要在数组中传递一个数组吗?
        猜你喜欢
        • 1970-01-01
        • 2018-09-01
        • 2011-03-12
        • 1970-01-01
        • 2018-10-28
        • 2020-11-13
        • 2017-05-23
        • 1970-01-01
        • 2018-08-16
        相关资源
        最近更新 更多