【问题标题】:cellForRowAt indexPath not called when give tableview height from contentSize从 contentSize 给出 tableview 高度时未调用 cellForRowAt indexPath
【发布时间】:2020-02-15 02:04:41
【问题描述】:

我不明白为什么cellForRowAt indexPath 不打电话。我检查了delegate, datasource 是正确的。而且我还检查了静态高度然后它会起作用,但是当我从 tableview 内容大小给 tableView 高度constant 时,我不会打电话。这是我的代码。

//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()
    }
}

【问题讨论】:

  • 设置出口表视图高度,如@IBOutlet weak var heightConstraintForTblItem: NSLayoutConstraint!
  • 如果表格视图高度小于单元格高度,则不会调用。请检查 tableview 高度。
  • 我认为您的 tableview 高度可能为零或小于单元格大小
  • @NiravKotecha 不,这是正确的
  • @JaydeepVyas 不,它不是零

标签: ios swift iphone uitableview contentsize


【解决方案1】:

试试这个

1) 删除func tableView(_ tableView: UITableView, estimatedHeightForRowAt方法和func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

2) 移除viewWillLayoutSubviews方法

3) 在ViewDidAppear

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

    }

3)在您的 API 调用之后或当您在 tableview 中加载数据时

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

确保给定正确的约束,确保正确连接 IBOutlet,仔细检查数据源委托

【讨论】:

  • 感谢您的帮助。我用我当前的代码尝试了它,它会工作。我刚刚在这个 viewWillLayoutSubviews() 方法中添加了 self.view.layoutIfNeeded() 并且工作正常
  • @KamleshShinarakhiya 是的,但我仍然建议您使用我与您共享的整个代码,因为有时您可能会面临由于某些多线程问题而无法正确调整表格视图的大小。如果您确信您的代码工作正常,那么您不需要更改它:) 如果需要,您可以标记答案已接受:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
相关资源
最近更新 更多