【问题标题】:uitableview willdisplaycell delegate is strangeuitableview会显示cell委托很奇怪
【发布时间】:2019-02-03 17:06:59
【问题描述】:

我一直在处理 tableview 并陷入以下问题。

据我了解,willDisplayCell 委托方法应该允许我访问当前的单元格设计。

我的问题是:它无法正常工作。尝试显示 80 个单元格时,该委托函数仅运行五次。不管这个函数以外的任何东西,在委托函数中写一行代码就可以很好地工作。

这是我的代码:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == comment_arr.count - 1 {
        print("end")
        // (1)self.comment_height.constant =  self.comment_table.contentSize.height + 30
    }
    // (2)self.comment_height.constant =  self.comment_table.contentSize.height + 30
}

如果 (1) 行存在,则它不起作用。但是如果存在 (2) 行,则效果很好。

如何让它工作?

【问题讨论】:

  • 尝试另一种方法来检测到达 tableview 的结尾。我不建议使用“willDisplayCell”,即使它对你有用,但出于多种原因..检查这个stackoverflow.com/questions/39015228/…跨度>
  • print("end") 是否打印过任何内容?如果不是,您的 if 语句中的逻辑可能是问题所在。

标签: ios swift xcode uitableview delegates


【解决方案1】:

首先获取section中的行数。然后找到最后一项

let totalRow = tableView.numberOfRows(inSection: indexPath.section)
        if(indexPath.row == totalRow - 1)
        {
           print("end")
            return
        }

【讨论】:

【解决方案2】:
tableView(_ tableView: UITableView, 
         heightForRowAt indexPath: IndexPath) -> CGFloat

在 willDisplayCell 之前调用

该主题在这里得到了完整而详细的介绍: TableView methods

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多