I have a tableView I am trying to populate with data. But the data is never loaded. The delegate method numberOfRowsInSection
is called, but cellForRowAt
isn't.
Interface Builder
Simulator Run
XCode Console
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
}
}