Uitable视图方法和等效的ViewDataSource方法之间的区别



如何:

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

不同于:

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?

我最近遇到了一个错误,我正在扩展UITableView,以便在给定的部分中迭代行。

func forRows(inSection section: Int, condition: (UITableViewCell) -> ()) {
    for row in 0..<numberOfRows(inSection: section) {
        let cell = cellForRow(at: IndexPath(row: row, section: section))!
        condition(cell)
    }
}

^^^不起作用,在最后一行的单元格

func forRows(inSection section: Int, condition: (UITableViewCell) -> ()) {
    for row in 0..<dataSource!.tableView(self, numberOfRowsInSection: section) {
        let cell = dataSource!.tableView(self, cellForRowAt: IndexPath(row: row, section: section))
        condition(cell)
    }
}

^^^有效。我明白了,但我不明白。

tableview委托方法及其在表格本身上的等效方法之间的内部有什么不同?

数据源是指提供单元格,并创建它们,但是您想创建它们。通常,这涉及将可重复使用的单元格出来或创建新细胞。

当您在表视图上调用cellForRow(at indexPath: IndexPath)时,它会为您提供一个已经存在的单元格,如果有一个。

  • a 委托方法(严格说话的 UITableViewDataSource也是A 委托),该框架要求某物或指示工作流程中的特定阶段。
    您不得自己调用委托方法

  • a 类或实例方法可以毫不犹豫地使用。

我不知道这是否是您的特定情况,因为我需要更多信息,但是

func cellForRow(at indexPath: IndexPath) -> UITableViewCell?

如果在屏幕上不可见该单元格,则返回nil。在另一种方法中,表将为您"加载"特定索引路径的单元格

相关内容

最新更新