我仍然获得在的tableView的错误,我想不通为什么:仍然得到错误空数组
@objc class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var productsToDisplay: [SKProduct]!
override func viewWillAppear(_ animated: Bool) {
// an assync call to load products to the productsToDisplay
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "PurchaseItemTableViewCell", for: indexPath) as? PurchaseItemTableViewCell else {
fatalError("Coulnd't parse table cell!")
}
// here the app always show an error without any specification
if(!(self.productsToDisplay != nil && self.productsToDisplay!.count > 0)) {
return cell
}
cell.nameLabel.text = "my text"
return cell
}
}
我做错了吗?或者在数据加载之前如何解决表的错误/未加载内容?
非常感谢您
如果它是零,你不会返回任何东西。在tableview委托方法部分使用numberOfRows并返回productsToDisplay.count。 – rMickeyD
抱歉,它在那里,我只是没有复制它 – David
@大卫为了安全起见,您应该返回'productsToDisplay?.count ?? 0'。 –