iosswiftiphoneuitableviewcontentsize

cellForRowAt indexPath not called when give tableview height from contentSize


I don't not why cellForRowAt indexPath not call. I checked delegate, datasource it is proper. And I also check with static height then It will work but when I gave tableView height constant from tableview content size then I will not call. This is my code.

//MARK: View life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        tblItemList.rowHeight = 76
        tblItemList.estimatedRowHeight =  UITableView.automaticDimension
    }

    override func viewWillLayoutSubviews() {
        heightConstraintForTblItem.constant = tblItemList.contentSize.height
    }

extension CheckoutVC : UITableViewDelegate, UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return arrProductList.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    { 
            let cell = tableView.dequeueReusableCell(withIdentifier: "CheckOutItemCell", for: indexPath) as! CheckOutItemCell
            if arrProductList.count > 0 {
                let data = arrProductList [indexPath.row]
                cell.lblItemName.text = data.product_name ?? ""
                cell.lblQuantity.text = "\(data.product_total_quantity ?? 0)"
                cell.lblTotalPrice.text = CURRENCY + "\(data.product_total_amount ?? 0.0)"
            }

            return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView == tblItemList {
            return UITableView.automaticDimension
        }
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }

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

Solution

  • Try this

    1) Remove func tableView(_ tableView: UITableView, estimatedHeightForRowAt method and func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

    2) Remove viewWillLayoutSubviews method

    3) in ViewDidAppear

     override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            self.view.layoutIfNeeded()
            self.heightConstraintForTblItem.constant = self. tblItemList.contentSize.height
            self.view.layoutIfNeeded()
    
        }
    

    3)After your API call or when you have data to load in tableview

                self.tblItemList.reloadData()
                self.view.layoutIfNeeded()
                self.heightConstraintForTblItem.constant = self. tblItemList.contentSize.height
                self.view.layoutIfNeeded()
    

    Make sure you have correct constraint given , Make sure you have correctly connected IBOutlet , Double check datasource delegate