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,533
  • 12
  • 39
  • 61
  • What exactly do you mean by "some cells the height is wrong"? What is the expected result, what happens instead? – mag_zbc Jul 18 '17 at 14:07
  • @mag_zbc I have to check 2 conditions.. if my in time2 is available I want to increase the height of the cell and increase the button's top constraint value. But when my condition doesnt satisfy cells are getting increased and top also getting increased. – user1960169 Jul 18 '17 at 14:09
  • @mag_zbc but not for all cells, this happens only for few cells. randomly – user1960169 Jul 18 '17 at 14:10
  • @mag_zbc And also when I scroll the tableview, then some correct cells also getting wrong size – user1960169 Jul 18 '17 at 14:13
  • Try to clear the constraints before recreating the height – Ortensia C. Jul 18 '17 at 14:56
  • @AdrianaCarelli where can I clear my constraints? – user1960169 Jul 18 '17 at 15:48
  • it would be better set the height in method: tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat – Ortensia C. Jul 18 '17 at 16:12

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

Ortensia C.
  • 4,666
  • 11
  • 43
  • 70