简体   繁体   English

uitableview willdisplaycell委托很奇怪

[英]uitableview willdisplaycell delegate is strange

I've been working around the tableview and got stuck on the following issue. 我一直在围绕tableview工作,并陷入以下问题。

As far as I understand, the willDisplayCell delegate method should allow me to access the current cell design. 据我了解, willDisplayCell委托方法应允许我访问当前的单元格设计。

My issue is: it does not work properly. 我的问题是:它无法正常工作。 That delegate function runs only five times when trying to display 80 cells. 当试图显示80个单元格时,该委托函数仅运行五次。 Regardless of anything other than this function, writing a line of code in the delegate function makes it work well. 不管该函数如何,在委托函数中编写一行代码都可以使其正常工作。

Here is my code: 这是我的代码:

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
}

If the (1) line exists, it doesn't work. 如果(1)行存在,则无法使用。 But if the (2) line exists, it works well. 但是,如果(2)行存在,则效果很好。

How do I get it to work? 我如何使它工作?

First get the number of rows in section. 首先获取部分中的行数。 Then find the last item 然后找到最后一个项目

let totalRow = tableView.numberOfRows(inSection: indexPath.section)
        if(indexPath.row == totalRow - 1)
        {
           print("end")
            return
        }
tableView(_ tableView: UITableView, 
         heightForRowAt indexPath: IndexPath) -> CGFloat

is called before willDisplayCell 在willDisplayCell之前被调用

the topic was covered in full and detailed way here: TableView methods 此处以完整和详细的方式介绍了该主题: TableView方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM