TableView - 未调用heightForRowAtIndexPath

Rya*_*yan 2 uitableview ios heightforrowatindexpath swift

在浏览了所有其他Stack Overflow表单后,我为我的一个单元格实现了动态高度,如下所示:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell

    if(indexPath.row == 1){
        var image : UIImage = maskRoundedImage(image: UIImage(named: "Temp_Profile.png")!, radius: 150)
        cell.imageView?.image = image
        return cell
    }
    cell.textLabel?.text = TableArray[indexPath.row]
    cell.backgroundColor = UIColor.init(colorLiteralRed: 0.56, green: 0, blue: 0.035, alpha: 1)
    cell.textLabel?.textColor = UIColor.white
    cell.textLabel?.font = UIFont(name: "Helvetica", size: 28)

    return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if  indexPath.row == 1 {
        return UITableViewAutomaticDimension * 3

    }
    return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)

看起来非常简单,但调试会话显示从未调用heightForRowAtIndexPath.View Controller是一个UITableViewController,其他一切都正常工作.你们有没有看到没有调用此功能的原因?

感谢您的帮助!

rma*_*ddy 13

在Swift 3中,签名是:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
Run Code Online (Sandbox Code Playgroud)

您有旧签名,因此无法识别.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 1 {
        return UITableViewAutomaticDimension * 3

    }
    return UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)

  • 对于任何试图使用UITableViewAutomaticDimension*3的人,请不要这样做.它只返回一个-1作为tableView的信号,使其成为默认值.使用实际数字 (3认同)

Nar*_*tty 6

对于我来说,我必须这样做。

self.tableviewObj.delegate = self
Run Code Online (Sandbox Code Playgroud)

因此,不要忘记将委托添加到self。因为heightForRowAtIndexparth是在委托协议中声明的。