Cell子视图约束在CellForRowAtIndex中以编程方式更改

时间:2017-07-18 14:02:49

标签: ios uitableview swift3 nslayoutconstraint custom-cell

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

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

        }

但有些细胞高度错误,请帮帮我

1 个答案:

答案 0 :(得分:0)

 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
         }

}

您可以按故事板构建多个原型单元

enter image description here

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

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