2017-07-18 42 views
0

我有一個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

回答

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

+0

你的意思是我不需要爲我的子視圖設置高度約束? – user1960169

+0

它是一個tableview,沒有必要設置高度約束,因爲在設置單元格高度時存在此方法 –

+0

我改變了你的方式。但現在所有的細胞都需要308個,這是我原來的細胞大小。 :( – user1960169

相關問題