简体   繁体   English

嵌入在 UIStackView 中的 UITableView 不会重新加载数据

[英]UITableView embedded in a UIStackView is not reloading data

I have a tableView I am trying to populate with data.我有一个 tableView 我想用数据填充。 But the data is never loaded.但数据永远不会加载。 The delegate method numberOfRowsInSection is called, but cellForRowAt isn't.委托方法numberOfRowsInSection被调用,但cellForRowAt不是。

Interface Builder界面生成器

在此处输入图片说明

Simulator Run模拟器运行

在此处输入图片说明

XCode Console XCode 控制台

在此处输入图片说明

View Debugger查看调试器

在此处输入图片说明

在此处输入图片说明

Can anyone see what I am doing wrong?谁能看到我做错了什么?

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
    }
}

Typically, when a table view never asks the data source for cells, it's because the table view has no size .通常,当表视图从不向数据源询问单元格时,这是因为表视图没有size I guess the runtime figures that since the table view has no size, it can't display any cells, so it doesn't need to ask for any.我猜运行时认为由于表格视图没有大小,所以它不能显示任何单元格,所以它不需要要求任何单元格。

In this particular case, your table view is inside a vertical stack view.在这种特殊情况下,您的表格视图位于垂直堆栈视图内。 A stack view, in some configurations, depends on the inherent height of its arranged subviews to know how to size them.在某些配置中,堆栈视图取决于其排列的子视图的固有高度,以了解如何调整它们的大小。 A label, for instance, has an inherent height (the height of its intrinsicContentSize ).例如,一个标签有一个固有的高度(它的intrinsicContentSizeheight )。

But a table view has no inherent height;但是 table view没有固有的高度; you have to give it one.你必须给它一个。 Otherwise, the table view's height will be zero — and that's why you don't see it, and why it isn't being asked for cells.否则,表格视图的高度将为零——这就是为什么你看不到它,以及为什么它不会被要求输入单元格。

Either you need to apply an absolute height constraint on the table view, or you need to implement the table view's intrinsicContentSize (or similar) in such a way as to determine the height.您需要在表格视图上应用绝对高度约束,或者您需要以确定高度的方式实现表格视图的intrinsicContentSize (或类似)。 The stack view will then take that into account, the table view will have height, the data source will be asked for cells, and you'll start to see something in the table view.然后堆栈视图将考虑到这一点,表格视图将具有高度,将要求数据源提供单元格,您将开始在表格视图中看到一些内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM