CollectionView在ViewDidload中没有ReloDdata,但它在ViewWillAppear中起作用

发布于 2025-02-01 14:21:43 字数 2836 浏览 3 评论 0原文

我从带有此代码的标签栏中显示了一个名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2025-02-08 14:21:43

如果您要在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文