我有一個表格視圖,它實作了我在我的應用程式中制作的自定義單元格。表格視圖從 firebase 獲取資料,并在視圖出現后重新加載。由于某種原因,表格視圖行沒有出現。這是為什么?
這是我的代碼
tableview實作方法
extension VendorItemsController:UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return VendorItems.Items.count
}
func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = table.dequeueReusableCell(withIdentifier:"cell",for: indexPath) as? ItemDisplayCell {
cell.DescriptionLabel?.text = VendorItems.Items[indexPath.row].itemDescription
cell.PriceLabel.text = VendorItems.Items[indexPath.row].price
// print(cell.PriceLabel.text)
return cell
}
return UITableViewCell()
}
我在哪里呼叫委托方法并重新加載我的資料
override func viewDidLoad() {
// if(auth.currentUser != nil){Progress}
// Do any additional setup after loading the view.
//no cnnection to table view omgggg
VendorItems = Vendor()
table.delegate = self
table.dataSource = self
//table.rowHeight = UITableView.automaticDimension
}
override func viewDidAppear(_ animated: Bool) {
//need to chheck if there are any documents here...
print(VendorItems.Items.count)
VendorItems.loadItemsData {
self.table.reloadData()
}
}
這是 Vendor 類中的 loadItemsMethod,每次視圖出現時都會運行
func loadItemsData(completed: @escaping () -> ()) {
db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
guard Error == nil else {
print("Error getting data")
return completed()
}
for document in QuerySnapshot!.documents {
print(document.data())
let item = Item.init(document.data())
self.Items.append(item)
}
}
}
這是我在表格視圖 cellForRowAt 方法中繼承的類
class ItemDisplayCell: UITableViewCell {
@IBOutlet weak var DescriptionLabel: UILabel!
@IBOutlet weak var PriceLabel: UILabel!
@IBOutlet weak var itemImage: UIImageView!
}
最后,這是我正在嘗試制作的原型單元
在此處輸入影像描述
對不起,我還沒有足夠的代表
uj5u.com熱心網友回復:
確保Cell Identifier在 Interface Builder 中設定原型單元格的屬性。您還可以在tableView: cellForRowAt方法中設定斷點以檢查單元格是否已出隊。
uj5u.com熱心網友回復:
你有沒有注冊你的手機?我可以看到您已設定代表但未注冊單元。如果您使用的是自定義單元格,那么您必須注冊您的自定義單元格。
如果您以編程方式創建了 tableview,則像這樣注冊單元格:
tableView.register(YourCustomCellName.self, forCellReuseIdentifier: CellId)
或者,如果您在情節提要中創建了 tableview 和情節提要中的單元格或 xib 注冊單元格,如下所示:
partnersListTableView.register(UINib(nibName: "YourCustomCellName", bundle: nil), forCellReuseIdentifier: CellId)
uj5u.com熱心網友回復:
您在方法 loadItemsData() 中犯了一個錯誤,有完成塊可以將其取回。您僅在錯誤情況下回傳此完成,但在成功情況下不回傳。所以,你也必須在這里退貨。
func loadItemsData(completed: @escaping (_ dataGet: Bool) -> void) {
db.collection("Vendor").document(currentUser.currentUser!.uid).collection("Items").addSnapshotListener {(QuerySnapshot, Error) in
guard Error == nil else {
print("Error getting data")
completed(false)
return
}
for document in QuerySnapshot!.documents {
print(document.data())
let item = Item.init(document.data())
self.Items.append(item)
completed(true)
}
}
}
使用 viewWillAppear() 的 viewDidAppear()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//need to chheck if there are any documents here...
VendorItems.loadItemsData { [weak self] dataGet in
if dataGet {
self?.table.reloadData()
} else {
print("Something went wrong")
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465805.html
上一篇:SwiftUI:對SwiftUI視圖中的NSManagedObject屬性更改做出反應?
下一篇:未能為開發準備設備
