在viewdidload()之前调用numberOfItemsInSection函数

时间:2018-03-14 10:10:42

标签: ios swift uicollectionview 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,我该怎么办呢,有人可以帮忙吗?

5 个答案:

答案 0 :(得分:1)

您不希望在ViewDidLoad之前调用collectionView吗?

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self
}

但是这不应该让你担心,因为如果你用来初始化CollectionView的数组是空的,那么如果调用转到numberOfItemsInSection方法就不重要了。

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

self.collectionView.reloadData()

答案 1 :(得分:0)

注释中的代码和数据源在代码中的建议并没有解决问题。

委托和数据源是在Interface Builder中连接还是在代码中连接是无关紧要的。 显然它们是连接的,否则numberOfItemsInSection根本就不会被调用。

由于默认情况下数据源数组不为空numberOfItemsInSection在加载视图后隐式调用 。那么你怎么知道什么时候调用viewDidLoad()而不打印消息?

retrieveData中,数据库请求以异步执行,因此您需要在检索数据后重新加载集合视图

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)
            self.collectionView.reloadData() // This line is crucial
            print("HELLLLLOOOO WOOOOORRRLLLDDDD")
        }
        else{
            print(error?.localizedDescription)
        }
    }
}

答案 2 :(得分:0)

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
}

}

答案 3 :(得分:0)

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
}

答案 4 :(得分:-2)

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

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

编辑:格式化函数

相关问题