简体   繁体   中英

Cell subviews constraints changing programmatically in CellForRowAtIndex

I have a UITableView custom cell. It has a UIView and 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

 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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM