【问题标题】:unable to Set Height of the CustomCells in swift无法快速设置 CustomCells 的高度
【发布时间】:2017-01-05 19:45:20
【问题描述】:

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

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()

}

我在单元格上定义了以下约束

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

下面是我的代码。

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

}

任何建议。 提前谢谢你。

【问题讨论】:

    标签: ios objective-c swift autolayout ios-autolayout


    【解决方案1】:

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

    UITableView dynamic cell heights only correct after some scrolling

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

    cell.layoutIfNeeded()
    

    【讨论】:

    • 有时还是会出现同样的问题......我该如何防止这种情况发生?
    • 有时?具体什么时候发生?
    • 上下滚动
    • 我对所有原型单元进行了限制,现在它工作正常。感谢您的帮助。
    猜你喜欢
    • 2017-02-20
    • 1970-01-01
    • 2013-07-16
    • 2017-02-04
    • 2018-08-04
    • 2011-04-02
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    相关资源
    最近更新 更多