ios - 无法快速设置 CustomCells 的高度

标签 ios objective-c swift autolayout ios-autolayout

我有很多 customCells,每个单元格可能有一个按钮,其作用类似于文本框或文本区域。当 View 加载时,它们的高度非常小。

override func viewDidLoad() {

    super.viewDidLoad();

    self.view.backgroundColor = UIColor.groupTableViewBackground
    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
   self.tableView.estimatedRowHeight = 100
   tableView.rowHeight = UITableViewAutomaticDimension
   self.tableView.setNeedsLayout()
   self.tableView.layoutIfNeeded()

}

我在单元格 enter image description here 上定义了以下约束

自动布局是否适用于原型(prototype)单元格? 如果不是如何实现呢? 我尝试使用 HeightforrowAtIndexPath 方法布局只有在滚动后才看起来不错。 View 的初始加载不会带有良好的布局。

下面是我的代码。

  func tableView(_ tableView: UITableView, heightForRowAt indexPath:   IndexPath) -> CGFloat {
   if (eleData.elementType=="apple")
    {
        return 90;
   }  
 else if (eleData.elementType=="banana")
    {
        return 50;
   }     
else  
{
  return 30;
}

}

任何建议。 提前谢谢你。

最佳答案

快速谷歌搜索给你你的答案......

UITableView dynamic cell heights only correct after some scrolling

如果需要,您需要在 cellForRowAtIndexPath 方法中返回单元格之前添加布局:

cell.layoutIfNeeded()

关于ios - 无法快速设置 CustomCells 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493432/

相关文章:

ios - 条件绑定(bind)错误极度迷惑

iOS8 - 在 UINavigationController 上推送 UIViewController 并旋转设备后,以前的 View Controller 中的大小错误

ios - 在 Apple 批准我的 API 请求之前,我是否可以在我的设备上编译和部署 NEHotspotHelper 项目?

iphone - 如何复制 CALayer?

objective-c - iPad+将图像选择器质量设置为低

ios - 快速将 CBUUID 转换为 NSNumber

php - 将一组 ID 从 Swift URLSession 传递到 PHP 以进行 SQL 查询?

ios - 这是 NSOperation 的有效 'main' 方法吗?

ios - 如何更改 xcode 中的包标识符

iphone - 浮点函数返回值