set tableview height dynamically and restrict to s

2019-08-26 09:53发布

问题:

i have one table view as popUpTableView, in that i need to set table view height as dynamically as per no of rows. i am adding in cellForRowAtIndexpath

  popupTableView.height = popupTableView.contentSize.height

it is working fine but the problem is when no of cells are increasing then the tableview height is increasing than its origingal height

extension DiagnosisViewController :UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableView == diagnosisTableView {
            return self.diagnosisModel.count
        } else if tableView == popupTableView {
            return self.popupDiagnosisModel.count
        }
        return 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        var cell:DiagnosisTableViewCell?

        if tableView == self.diagnosisTableView {

            cell = self.diagnosisTableView.dequeueReusableCell(withIdentifier: "DiagnosisCell") as? DiagnosisTableViewCell



            if diagnosisModel.count > 0 {

                let model = diagnosisModel[indexPath.row]
                cell?.indexLabel.text =  String(indexPath.row + 1)
                cell?.diagnosisNameLabel.text = "  \(model.diagnosisDescription!)"
                cell?.deleteButton.tag = indexPath.row
                cell?.deleteButton.addTarget(self, action:#selector(deleteDiagnosis(button:)), for: .touchUpInside)
                diagnosisNotFoundLabel.isHidden = true

            } 

        } else if tableView == self.popupTableView {


            cell = self.diagnosisTableView.dequeueReusableCell(withIdentifier: "DiagnosisCell") as? DiagnosisTableViewCell

            cell?.deleteButton.isHidden = true

            if (indexPath.row % 2 == 0) {
                cell?.baseView.backgroundColor = UIColor.white
            }
            else {
                cell?.baseView.backgroundColor = UIColor.init(colorLiteralRed: 241/255, green: 241/255, blue: 241/255, alpha: 1.0)
            }

            cell?.indexLabel.text =  String(indexPath.row + 1)
            cell?.indexView.layer.cornerRadius = (cell?.indexView.frame.size.width)!/2
            cell?.indexView.layer.borderWidth = 0.5
            cell?.indexView.layer.borderColor = UIColor.lightGray.cgColor

            cell?.baseView.layer.borderWidth = 0.5
            cell?.baseView.layer.cornerRadius = 5.0
            cell?.baseView.layer.borderColor = UIColor.lightGray.cgColor;

            if diagnosisModel.count > 0 {

                let model = popupDiagnosisModel[indexPath.row]
                cell?.diagnosisNameLabel.text = "  \(model.diagnosisDescription!)"
            }
            self.popupTableView.height = self.popupTableView.contentSize.height 

        }
        return cell!
    }

}


let maxHeight = popupTableView.frame.size.height
let changeHeight = popupTableView.contentSize.height

if changeHeight < maxHeight {
popupTableView.height = changeHeight
}

how to get tableview height for only visible cells and where to place that code ??

Thanks in Advance.

回答1:

Use this in your viewdidLoad() of the popuptableview:

popupTableView.estimatedRowHeight = 120

and create a function for setting height for automatic dimension :

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}


回答2:

To get the dynamic height for UITableViewCell return UITableViewAutomaticDimension as your row height .

Example : ( In My Case )

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
        {
            if indexPath.row == 0
            {
                return 200
            }
            else if indexPath.row == 1
            {
                return 10
            }
            else
            {
                return UITableViewAutomaticDimension
            }
        }

In that case don't forget to use AutoLayout , For more Info See Here



回答3:

Try adding the code to adjust the height in viewWillLayoutSubviews method. Try this

override func viewWillLayoutSubviews() {
  super.viewWillLayoutSubviews()
  let maxHeight = popupTableView.frame.size.height
  let changeHeight = popupTableView.contentSize.height

  if changeHeight < maxHeight {
  popupTableView.height = changeHeight
  }
}


回答4:

take one variable which will caluculate bottomconstraint value of tableview

 var bottomConstraintValue : CGFloat = 0.0
    override func viewDidLoad() {
        super.viewDidLoad()
   bottomConstraintValue = self.popupContainerView.height - self.popupTableView.frame.origin.y - self.popupTableView.frame.size.height
     }

call delegate method willDisplay: of tableview

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

if tableView == popupTableView {
        if indexPath.row == (popupTableView.indexPathsForVisibleRows?.last)?.row {
            adjustTableViewHieght(popupTableView: self.popupTableView,popupContainerView: self.popupContainerView , bottomConstraintValue: self.bottomConstraintValue)
        }
    }
}

    func adjustTableViewHieght(popupTableView: UITableView,popupContainerView: UIView , bottomConstraintValue:CGFloat){
        let contentSizeHeight = popupTableView.contentSize.height;
        let tableViewHieghtWithBottomSpace = popupContainerView.height - popupTableView.frame.origin.y
        let actualTableViewHieght = (tableViewHieghtWithBottomSpace - bottomConstraintValue)
        if (contentSizeHeight > actualTableViewHieght) {
            popupTableView.height = actualTableViewHieght
        } else {
            popupTableView.height = contentSizeHeight
        }
    }