numberOfItemsInSection 函数在 viewdidload() 之前调用

numberOfItemsInSection func calling before viewdidload()

在这里,我首先从 firebase 中检索图像,然后将其添加到 locationImage 数组,稍后将添加到 collectionView。

import UIKit
import Firebase
import FirebaseStorage

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var locationImage = [UIImage(named: "hawai"), UIImage(named: "mountain")]

override func viewDidLoad() {
    super.viewDidLoad()

    retrieveData()
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    print(locationImage.count)
    return locationImage.count
}
func retrieveData(){
    let database = FIRDatabase.database().reference()
    let storage = FIRStorage.storage().reference()

    let imageRef = storage.child("blue blurr.png")

    imageRef.data(withMaxSize: (1*1000*1000)) { (data, error) in
        if error == nil{

            let tempImage = UIImage(data: data!)
            self.locationImage.append(tempImage)
            print("HELLLLLOOOO WOOOOORRRLLLDDDD")
            print(self.locationImage.count)
        }
        else{
            print(error?.localizedDescription)
        }
    }
    return
}

}

这里 retrieveData() 函数在 collectionView() 之前调用 。应该先调用 viewdidload,我该怎么做,有人可以帮忙吗?

您不想在 ViewDidLoad 之前调用 collectionView 吗?

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self
}

但这不应该让您担心,因为如果您用来初始化 CollectionView 的数组是空的,那么调用 numberOfItemsInSection 方法也没关系。

这里您需要的是在您的 locationImage 中有数据后调用重新加载。所以在你的 self.locationImage.append(tempImage) 之后,添加:

self.collectionView.reloadData()
import UIKit
import Firebase
import FirebaseStorage

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var locationImage = [UIImage(named: "hawai"), UIImage(named: "mountain")]

override func viewDidLoad() {
    super.viewDidLoad()

    retrieveData()

    //Add these two line here remove delegate and datasource from storyborad
    collectionView.dataSource = self
    collectionView.delegate = self
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    print(locationImage.count)
    return locationImage.count
}
func retrieveData(){
    let database = FIRDatabase.database().reference()
    let storage = FIRStorage.storage().reference()

    let imageRef = storage.child("blue blurr.png")

    imageRef.data(withMaxSize: (1*1000*1000)) { (data, error) in
        if error == nil{

            let tempImage = UIImage(data: data!)
            self.locationImage.append(tempImage)
            print("HELLLLLOOOO WOOOOORRRLLLDDDD")
            print(self.locationImage.count)
        }
        else{
            print(error?.localizedDescription)
        }
    }
    return
}

}

100% 工作

UICollectionView 的 IBOutlet 在哪里?

 @IBOutlet weak var collectionView: UICollectionView!

首先设置 IBOutlet 并尝试此代码:

import UIKit
import Firebase
import FirebaseStorage

 class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

var locationImage = [UIImage(named: "hawai"), UIImage(named: "mountain")]

override func viewDidLoad() {
super.viewDidLoad()
 //Add these two line here remove delegate and datasource from storyborad
collectionView.dataSource = self
collectionView.delegate = self
retrieveData()
}


 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

print(locationImage.count)
return locationImage.count
 }
 func retrieveData(){
let database = FIRDatabase.database().reference()
let storage = FIRStorage.storage().reference()

let imageRef = storage.child("blue blurr.png")

imageRef.data(withMaxSize: (1*1000*1000)) { (data, error) in
    if error == nil{

        let tempImage = UIImage(data: data!)
        self.locationImage.append(tempImage)
        print("HELLLLLOOOO WOOOOORRRLLLDDDD")
        print(self.locationImage.count)
    }
    else{
        print(error?.localizedDescription)
    }
}
return
}

一个简单的解决方案是像这样让您的函数休眠几秒钟

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    do {
        sleep(1)
    }

编辑:格式化函数