我仍然獲得在的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'。 –