代码之家  ›  专栏  ›  技术社区  ›  Hyunmin Jung

UITableView将显示Cell委托异常

  •  0
  • Hyunmin Jung  · 技术社区  · 7 年前

    我一直在围绕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)行存在,它就工作得很好。

    我怎样才能让它工作?

    2 回复  |  直到 7 年前
        1
  •  0
  •   Syed Sadrul Ullah Sahad    7 年前

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

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

    在willDisplayCell之前调用

    本专题在这里详细地介绍了: TableView methods

    推荐文章