CollectionView在ViewDidload中没有ReloDdata,但它在ViewWillAppear中起作用
我从带有此代码的标签栏中显示了一个名为newordervc的ViewController:
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下载了一些代码。 newordervc:
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()
}
}
谢谢您的任何帮助。
I showed a viewController named NewOrderVC from a tab bar with this code:
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)
}
With getStore function, I download some codes from firebase.
NewOrderVC:
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
In viewDidLoad, collectionView.reloadData does not work. I can't see collectionView when view controller appeared.
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
If I change the viewController with using tab bar icons, collectionView appear and objects loaded in it.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.delegate = self
collectionView.dataSource = self
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
This is getStore function. It works like this.
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()
}
}
Thank you for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要在Collection View中显示的数据来自GetStore函数,那么您需要在数据到达之后重新加载CollectionView,然后在ViewDidload和ViewWillAppear中进行重新加载。
If the data you are displaying in collectionView is coming from getStore function, then you need to reload CollectionView after the data comes rather then in viewDidLoad and ViewWillAppear