数据不会填充UICollectionView滚动视图 [英] Data won't populate the UICollectionView scroll view

查看:0
本文介绍了数据不会填充UICollectionView滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此还是个新手,所以请对我宽容一点!

我的问题如下:

我正在尝试使用Firebase中的数据填充滚动视图(UICollectionView)。

我确信它正在成功检索数据,因为我可以使用Firebase函数末尾的for循环打印数据。

问题是,当我尝试将数据插入滚动视图时,系统提示数据超出范围!!

有人向我指出,ScrollView是用我在下面显示的scllView委托方法填充的。

还有人向我指出,我的数据源是‘self.ages’。如何将从Firebase检索到的数据传递给委托方法?

Fatal error: Index out of range

以下是我的代码:

//arrays of names and descriptions which are populated in firebase() function
    var names:[String] = []
    var descriptions: [String] = []

页面类:

 let imageName: UIImage
 let headerText: String
 let bodyText: String 

这是视图DidLoad

override func viewDidLoad() {
        super.viewDidLoad()

        firebase()
        setupImages()
    }

图片设置函数如下:

 func setupImages(){

        self.pages = [
            Page(imageName: self.image, headerText: names[0], bodyText: descriptions[0]),

            Page(imageName: self.image, headerText: names[1], bodyText: descriptions[1]),

            Page(imageName: self.image, headerText: names[2], bodyText: descriptions[2]),

            Page(imageName: self.image, headerText: names[3], bodyText: descriptions[3]),

            Page(imageName: self.image, headerText: names[4], bodyText: descriptions[4]),
        ]

        self.collectionView?.backgroundColor = .white
        self.collectionView?.register(PageCell.self, forCellWithReuseIdentifier: "cellId")
        self.collectionView?.isPagingEnabled = true
    }

我认为该问题是由于检索Firebase数据不够快引起的。

我这样说是因为我在另一个类中有另一个似乎可以设置它的函数:

override init(frame: CGRect) {
        super.init(frame: frame)
        //setup layout sets up constraints for the layout of the page
        setupLayout()
    }

添加UICollectionViewDataSource方法的:

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pages.count
    }

和:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PageCell

        let page = pages[indexPath.item]
        cell.page = page
        return cell
    }

Firebase取数方式:

func firebase()
    {
        //connection to firebase for the names and descriptions
        let db = Firestore.firestore()
        db.collection(restaurauntName).getDocuments { (snapshot, err) in

            if let err = err {
                print("Error getting documents: (err)")
            } else {
                for document in snapshot!.documents {
                    let name = document.get("Name") as! String
                    let description = document.get("Description") as! String
                    //Add names and descriptions to the arrays
                    self.names.append(name)
                    self.descriptions.append(description)
                }
            }

            for x in self.names{
                print(x)
            }

        }
    }

感谢您的阅读,如果有人能帮我找到正确的方向,我将不胜感激!

推荐答案

问题是这样的,您的假设是正确的;代码没有按正确的顺序流动。

override func viewDidLoad() {
   super.viewDidLoad()

   firebase()
   setupImages()
}
Firebase是异步的,您的应用程序需要以一种只尝试在调用后的闭包内使用Firebase数据的方式来构建。在您的viewDidLoad函数中,您调用Firebase()来加载数据,但setupImages函数将在Firebase有时间从服务器获取数据之前实际执行。

幸运的是,这是一个简单的解决办法。以下是一些更新了名称的新函数,以使流程更清晰

override func viewDidLoad() {
   super.viewDidLoad()

   self.loadDataFromfirebase()
}

然后

func loadDataFromFirebase() {
   let db = Firestore.firestore()
   db.collection(restaurauntName).getDocuments { (snapshot, err) in
      if let err = err {
         print("Error getting documents: (err)")
         return
      } else {
         for document in snapshot!.documents {
            let name = document.get("Name") as! String
            let description = document.get("Description") as! String
            self.names.append(name)
            self.descriptions.append(description)
         }
         self.setupImages() //safe to do this here as the firebase data is valid
         self.collectionView.reloadData()
      }
   }
}

我担心的是如果有25份文件...您的setupImages仅访问前5个文档。同样,如果有3个文档,应用程序将崩溃,因为该函数将尝试访问索引3、4处的对象,而这些对象将不存在。

这篇关于数据不会填充UICollectionView滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆