Search code examples
iosiphoneswiftuitableviewrow-height

UITableview Auto Adjust Row Issue Swift IOS?


I am facing one issue with adjusting UITableView row hight dynamically. I know by using this methods we can achieve

self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0; 

but in my case I am using XIB file for UITableViewCell and I am adding shadow for cells. in this case how to add row height dynamically. At the same time, based on server time I am showing and hiding buttons. So please can anyone suggest me how to fix this issue. This is my cell for row index method.

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "Custom"
        var cell: PApplyLeaveTableViewCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
        if cell == nil {
            tableView.register(UINib(nibName: "PApplyLeaveTableViewCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? PApplyLeaveTableViewCell
        }
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.contentView.backgroundColor = UIColor.clear
        var localDic :NSDictionary!
        localDic = totlLeavesArray.object(at: indexPath.row) as! NSDictionary
        cell.acknowled_lbl.text = localDic["acknowledgement"] as? String
        cell.date_lbl.text = localDic["totalDate"] as? String
        cell.reason_lbl.text = localDic["reason"] as? String
        let compareDate = localDic["compare"] as? String
        if(compareDate == "No")
        {
            cell.delet_Btn.isHidden = true
            cell.edit_Btn.isHidden = true
        }
        else
        {
            cell.delet_Btn.isHidden = false
            cell.edit_Btn.isHidden = false
        }
        cell.edit_Btn.tag = indexPath.row
        cell.edit_Btn.addTarget(self, action: #selector(PApplyLeaveViewController.EditViewAction(_:)), for:.touchUpInside)
        cell.delet_Btn.tag = indexPath.row

        cell.delet_Btn.addTarget(self, action: #selector(PApplyLeaveViewController.DeleteAction(_:)), for:.touchUpInside)
        let whiteRoundedView : UIView = UIView(frame: CGRect(x: 5, y: 8, width: self.view.frame.size.width - 15, height: 220))
        whiteRoundedView.layer.backgroundColor = CGColor(colorSpace: CGColorSpaceCreateDeviceRGB(), components: [1.0, 1.0, 1.0, 0.9])
        whiteRoundedView.layer.masksToBounds = false
        whiteRoundedView.layer.cornerRadius = 2.0
        whiteRoundedView.layer.shadowOffset = CGSize(width: -1, height: 1)
        whiteRoundedView.layer.shadowOpacity = 0.2
        cell.contentView.addSubview(whiteRoundedView)
        cell.contentView.sendSubview(toBack: whiteRoundedView)
        cell.contentView.backgroundColor = UIColor.clear
        return cell
    } 

Solution

  • i resolved my issue by setting constraints values. i taken label bottom constraint to "bottom space to container". This is my code for handling buttons show and hidden. But i couldn't able to fix row height accordingly. but somehow i am fixing design problem.

    if compareDateString == currentDateString
            {
                cell.delete_Btn.isHidden = true
                cell.edit_Btn.isHidden = true
                cell.bottomConstraint.constant = 30
            }
    
            else if compareDateString! < currentDateString
            {
                cell.delete_Btn.isHidden = true
                cell.edit_Btn.isHidden = true
              cell.bottomConstraint.constant = 30
            }
            else
            {
                cell.delete_Btn.isHidden = false
                cell.edit_Btn.isHidden = false
                cell.bottomConstraint.constant = 45
    
            }