UICollectionView随机无法获取所有数据

时间:2018-03-07 16:08:16

标签: swift uicollectionview

我的课程如下

class xxx: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

我从Firebase获取数据的方式如下:

func loadUserImages(){


    ref.child("people/\(profile.userID)/posts").observe(.value, with: { (imagesSnapshot) in


        //save users to dictionary
        let imagesDictionary = imagesSnapshot.value as! NSDictionary


        //get all users posts
        for(id,_) in imagesDictionary{
            let userImgRef = self.ref.child("posts").child(id as! String)
            print(userImgRef)

            //userPostRef.observe(.childAdded, with: { (postSnapshot) in
            userImgRef.observe(.value, with: { (imageSnapshot) in
                if let imageData = imageSnapshot.value as? NSDictionary{



                    guard let imageURL = imageData["thumb_url"] as! String! else {return}
                    print(imageURL)
                    guard let imageText = imageData["text"] as! String! else {return}
                    print(imageText)
                    // https://www.dotnetperls.com/2d-swift
                    var row = [String]()
                    row.append(imageURL)
                    row.append(imageText)
                    //append to feed array
                    self.picturesArray.append(row)
                    print(self.picturesArray.count)
                    //reload the collection view
                    self.accountThreeCollectionView.reloadData()
                }
            })
        }
    })

这是由didload调用的。

有时我的

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

只收到第一行,然后崩溃。

这是非常随意的。我做错了什么?

UPDATE: As per request

线程1:致命错误:索引超出范围

accountCellThree.verifiedPictures.sd_setImage(with: URL(string: postsArray[indexPath.row][0]), placeholderImage: UIImage(named: "ic_photo_camera_white"))

中的

这是因为对db的调用无法及时获取所有数据,所以我只有一行而不是全部。

在调用之前我是否必须使用完成处理程序?如果是这样,怎么样?

0 个答案:

没有答案