首页 > 解决方案 > UITableviewCell 在动态设置高度为零时卡住滚动

问题描述

我有一个 JSON 对象,其中一个键是 interface_id,我想相应地设置单元格高度,当我尝试设置高度 0 时,tableView 在滚动时卡住了。

public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
   if (dataArray[indexPath.row] as AnyObject).value(forKey: "interface_id") as! Int == 1{
     return 0
   }else{
    return UITableViewAutomaticDimension
  }
}

标签: iosswiftuitableview

解决方案


基本上,要使用自我调整大小的单元格,UITableView您将设置估计的行高,并将 rowHeight 设置为UITableViewAutomaticDimension. 所以改变行的单元格高度你的代码应该是这样的。

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44.0
    }

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (dataArray[indexPath.row] as AnyObject).value(forKey: "interface_id") as! Int == 1{
        return 0
    }else{
        return UITableViewAutomaticDimension
    }
}

推荐阅读