【问题标题】:numberOfItemsInSection func calling before viewdidload()在 viewdidload() 之前调用 numberOfItemsInSection 函数
【发布时间】:2018-03-14 10:10:41
【问题描述】:

在这里,我首先从 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,我该怎么做,有人可以帮忙吗?

【问题讨论】:

  • 在retrieveData() 函数中重新加载collectionview。
  • 你想达到什么目的?
  • 获取数据后在viewDidLoad中设置collectionView数据源。不要在情节提要中设置
  • 从 IB 中删除数据源和委托,并在加载数据后将它们设置在检索数据中。 @Qazi Shahood
  • ViewDidLoad 绝对不应该首先被调用。这是预期的行为。它异步运行。您应该在加载控制器之前传递数据...

标签: ios swift uicollectionview viewdidload


【解决方案1】:

您不想在 ViewDidLoad 之前调用 collectionView?

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self
}

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

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

self.collectionView.reloadData()

【讨论】:

    【解决方案2】:
    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】:

      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】:

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

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

        编辑:格式化函数

        【讨论】:

        • 永远不要那样做!!
        猜你喜欢
        • 1970-01-01
        • 2014-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多