我使用以下代碼從標簽欄中顯示了一個名為 NewOrderVC 的視圖控制器:
if let selectedAnn = mapView.selectedAnnotations[0] as? StoreAnnotation {
let id = selectedAnn.storeModel!.id
let vc = NewOrderVC(storeID: id)
vc.modalPresentationStyle = .fullScreen
self.show(vc, sender: nil)
}
使用 getStore 功能,我從 firebase 下載了一些代碼。新訂單VC:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
self.getStore(id: self.id)
}
initializeHideKeyboard()
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 1
layout.minimumInteritemSpacing = 1
layout.itemSize = CGSize(width: view.width / 2 - 15,
height: view.width - 30)
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
guard let collectionView = collectionView else {
return
}
collectionView.register(ProductCollectionViewCell.self, forCellWithReuseIdentifier: ProductCollectionViewCell.identifier)
collectionView.register(HeaderCollectionReusableView.self,
forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader,
withReuseIdentifier: HeaderCollectionReusableView.identifier)
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
view.addSubview(finishButton)
collectionView.backgroundColor = .systemBackground
在 viewDidLoad 中,collectionView.reloadData 不起作用。當視圖控制器出現時,我看不到 collectionView。
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
如果我使用標簽欄圖示更改 viewController,則會出現 collectionView 并在其中加載物件。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.delegate = self
collectionView.dataSource = self
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
這是 getStore 函式。它是這樣作業的。
private func getStore(id: String) {
DatabaseManager.shared.showProducts(id: id) { downloaded in
switch downloaded {
case .failure(_):
self.makeAlert(title: "Error", message: "Products could not download!")
break
case.success(let products):
self.store = products
}
}
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
感謝您的任何幫助。
uj5u.com熱心網友回復:
如果您在 collectionView 中顯示的資料來自 getStore 函式,那么您需要在資料到來后重新加載 CollectionView 而不是在 viewDidLoad 和 ViewWillAppear 中
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/481982.html
