0

I have a UITableViewcustom cell. It has a UIViewand a UIButton I want to change this view height constraint and the button's top constraint programmatically in my `cellForRowAtIndex. So Inside this im doing like this.

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

        }

But some cells the height is wrong, please help me

user1960169
  • 3,363
  • 10
  • 37
  • 56

1 Answers1

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
         }

}

You can build multiple prototype cell by storyboard

enter image description here

Each type of cell creates it with a different height and with identifier different and not use constraints

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

Adriana Carelli
  • 4,406
  • 11
  • 42
  • 70