嵌入在 UIStackView 中的 UITableView 没有重新加载数据

UITableView embedded in a UIStackView is not reloading data

我有一个 tableView,我正在尝试填充数据。但是数据永远不会加载。委托方法 numberOfRowsInSection 被调用,但 cellForRowAt 未被调用。

界面生成器

模拟器运行

XCode 控制台

查看调试器

谁能看出我做错了什么?

class ChargeDetailsView: UIView {

    @IBOutlet weak var chargesTable: UITableView!
    @IBOutlet weak var tvSubtotalAmount: UILabel!
    @IBOutlet weak var tvDiscountsAmount: UILabel!
    @IBOutlet weak var tvTaxesAmount: UILabel!
    @IBOutlet weak var tvGrandTotalAmount: UILabel!
    
    private var allCharges:[ServiceLineItem] = []
    
    class func instanceFromNib() -> UIView {
        return UINib(nibName: "ChargeDetailsView", bundle: Bundle(for: ChargeDetailsView.self)).instantiate(withOwner: nil, options: nil)[0] as! UIView
    }
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        print("ChargeDetailsView awakeFromNib")
        self.chargesTable.delegate = self
        self.chargesTable.dataSource = self
        self.chargesTable.register(ServiceLineItemCell.nib(), forCellReuseIdentifier: ServiceLineItemCell.identifier)
        
        self.tvSubtotalAmount.text = "-"
        self.tvDiscountsAmount.text = "-"
        self.tvTaxesAmount.text = "-"
        self.tvGrandTotalAmount.text = "-"
        
    }
    
    public func configure(with details: GetReceiptDetails){
        self.tvSubtotalAmount.text = "-"
        self.tvDiscountsAmount.text = "-"
        self.tvTaxesAmount.text = "-"
        self.tvGrandTotalAmount.text = "-"
        
        if let priceSubtotal = details.priceSubtotal {
            self.tvSubtotalAmount.text = String(format: "$%.02f", Double(priceSubtotal))
        }
        
        if let priceDiscount = details.priceDiscount {
            self.tvDiscountsAmount.text = String(format: "$%.02f", Double(priceDiscount))
        }
        
        if let priceTax = details.priceTax {
            self.tvTaxesAmount.text = String(format: "$%.02f", Double(priceTax))
        }
        
        if let priceTotal = details.priceTotal {
            self.tvGrandTotalAmount.text = String(format: "$%.02f", Double(priceTotal))
        }
        
        if details.pricing != nil, let items = details.pricing?.lineItems, items.count > 0 {
            allCharges.removeAll()
            allCharges.append(contentsOf: items)
            print("allCharges total items: \(allCharges.count)")
        
            self.chargesTable.reloadData()
        }
    }
}


extension ChargeDetailsView: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("ChargeDetailsView numberOfRowsInSection: \(allCharges.count)")
        return allCharges.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("row: \(indexPath.row)")
        let cell = tableView.dequeueReusableCell(withIdentifier: ServiceLineItemCell.identifier) as! ServiceLineItemCell
        cell.configure(with: allCharges[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

通常,当 table 视图从不向数据源询问单元格时,这是因为 table 视图没有 size。我猜运行时认为由于 table 视图没有大小,它不能 显示 任何单元格,所以它不需要 询问 任何。

在这种特殊情况下,您的 table 视图位于垂直堆栈视图中。在某些配置中,堆栈视图取决于其排列的子视图的固有高度,以了解如何调整它们的大小。例如,标签具有固有高度(其 intrinsicContentSizeheight)。

但是 table 视图 没有 固有高度;你必须给它一个。否则,table 视图的高度将为零 — 这就是您看不到它的原因,也是单元格不要求它的原因。

您需要在 table 视图上应用绝对高度约束,或者您需要以这样的方式实现 table 视图的 intrinsicContentSize(或类似)确定高度。然后堆栈视图将考虑到这一点,table 视图将具有高度,将要求数据源提供单元格,您将开始在 table 视图中看到一些东西。