CellForRowAtIndex中以编程方式更改单元格子视图约束

问题描述:

我有一个UITableView自定义单元格。它有一个UIView和一个UIButton 我想改变这个视图高度约束和按钮的顶部约束在我的`cellForRowAtIndex编程。所以在这里,我这样做。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 

     } 

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

+0

你是什么意思,“有些细胞的高度是错误的”?预期的结果是什么,取而代之的是什么? –

+0

@mag_zbc我必须检查2个条件..如果我的time2可用,我想增加单元格的高度并增加按钮的最高限制值。但是当我的条件不满足时,细胞正在增加,最高也在增加。 – user1960169

+0

@mag_zbc但不适用于所有单元格,这种情况仅适用于少数单元格。随机 – user1960169

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

+0

你的意思是我不需要为我的子视图设置高度约束? – user1960169

+0

它是一个tableview,没有必要设置高度约束,因为在设置单元格高度时存在此方法 –

+0

我改变了你的方式。但现在所有的细胞都需要308个,这是我原来的细胞大小。 :( – user1960169