【问题标题】:Cell subviews constraints changing programmatically in CellForRowAtIndex单元格子视图约束在 CellForRowAtIndex 中以编程方式更改
【发布时间】:2017-07-18 14:02:49
【问题描述】:

我有一个UITableViewcustom 单元格。它有一个UIView和一个UIButton 我想在我的`cellForRowAtIndex.xml 中以编程方式更改此视图高度约束和按钮的顶部约束。所以在这里面我是这样做的。

if(myintime2 == "00:00" && myoutTime2 == "00:00"  )
        {
            cell.btnIntime2.setTitle(myintime2, for: .normal)
            cell.btnOutTime2.setTitle(myoutTime2, for: .normal)

            cell.btnOutTime2.isHidden=true
            cell.btnIntime2.isHidden=true
            cell.customViewHeightConstraint.constant = 111
            cell.customBtnIn1Top.constant = 111/2


        }

        else if (myintime2 == "N/A" && myoutTime2 == "N/A")
        {
            cell.btnIntime2.setTitle(myintime2, for: .normal)
            cell.btnOutTime2.setTitle(myoutTime2, for: .normal)

            cell.btnOutTime2.isHidden=true
            cell.btnIntime2.isHidden=true
            cell.customViewHeightConstraint.constant = 111
            cell.customBtnIn1Top.constant = 111/2

        }


        else
        {
            cell.btnIntime2.setTitle(myintime2, for: .normal)
            cell.btnOutTime2.setTitle(myoutTime2, for: .normal)
            cell.customViewHeightConstraint.constant = 222
            cell.customBtnIn1Top.constant = 10
            cell.btnOutTime2.isHidden=false
            cell.btnIntime2.isHidden=false

        }

但是有些单元格的高度不对,请帮帮我

【问题讨论】:

  • “某些单元格的高度错误”到底是什么意思?预期的结果是什么,会发生什么?
  • @mag_zbc 我必须检查 2 个条件。如果我的 in time2 可用,我想增加单元格的高度并增加按钮的顶部约束值。但是当我的条件不满足时,细胞越来越多,顶部也越来越大。
  • @mag_zbc 但并非适用于所有单元格,这只发生在少数单元格中。随机
  • @mag_zbc 而且当我滚动表格视图时,一些正确的单元格也会得到错误的大小
  • 在重新创建高度之前尝试清除约束

标签: ios uitableview swift3 nslayoutconstraint custom-cell


【解决方案1】:
 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

         if(myintime2 == "00:00" && myoutTime2 == "00:00"  ){
                return 111
         }
        else if (myintime2 == "N/A" && myoutTime2 == "N/A")
        {
                return 111
         }else{
               return 222
         }

}

您可以通过情节提要构建多个原型单元

每种类型的单元格都使用不同的高度和标识符创建它,并且不使用约束

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

【讨论】:

  • 你的意思是我不需要为我的子视图设置高度限制?
  • 它是一个tableview,不需要设置高度约束,因为在设置单元格高度时存在这个方法
  • 我以你的方式改变了它。但是现在所有的单元格都占用了 308,这是我原来的单元格大小。 :(
  • 我在 heightforrowatIndex 里面改变了,现在高度在改变,但我不明白如何更新子视图的约束
  • 我在 heightforrowatIndex 里面改变了,现在高度在改变,但我不明白如何更新子视图的约束
猜你喜欢
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多