2017-07-17 58 views
0

試圖用這種方法來加載在集合視圖數據火力地堡存儲的數據顯示兩次

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if urls.count == 0 { 
     return UICollectionViewCell() 
    } 
    let url = urls[indexPath.item] 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, 
                for: indexPath) as! ImageCollectionViewCell 
    storageRef.reference(withPath: url).getData(maxSize: 15 * 1024 * 1024, completion: { data, error in 
      if let data = data {     
       cell.imageView.image = UIImage(data: data) 
      } 
     }) 
    return cell 
} 

的事情是 - 起初我還可以看到兩個單元沒有數據,然後完成被調用,一個我得到一個數據,但兩個單元格中的第一個圖像。 我該如何做對嗎?我怎樣才能在同一時間獲取元數據呢?

截圖:

Screenshot:

UPD:

我寫陣列

var datas = ["first", "second", "third", "fourth"] 

一種改變細胞代碼

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, 
                for: indexPath) as! ImageCollectionViewCell 
    cell.nameLabel.text = datas[indexPath.item] 
    return cell 
} 

並得到了這一點:

enter image description here

還是看不到有什麼不對。我的過分的方法:

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 2 
} 

func collectionView(_ collectionView: UICollectionView, 
        numberOfItemsInSection section: Int) -> Int { 
    return datas.count/2 
} 

解決。應該返回numberOfSections = 1

+0

可以添加你的截圖呢? –

+0

@TalhaQ我會,但我不認爲這將是有用的 – L1GhTUA

+0

@TalhaQ,如問 – L1GhTUA

回答

0

我認爲問題是,你下載的信息在cellForItemAt indexPath:功能,你應該下載的圖像在另一個函數這樣

//Variable to store the UIImages 
    var images = [UIImage]() 

//function to download the images, run it in the viewdidload like self.getImages() 
    func getImages(){ 

     for url in self.urls{ 

      storageRef.reference(withPath: url).getData(maxSize: 15 * 1024 * 1024, completion: { data, error in 
       if let data = data { 
        self.images.append(UIImage(data: data)) 
       } 
      }) 
     } 

     self.YourCollectionView.reloadData() 

    } 


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     if urls.count == 0 { 
      return UICollectionViewCell() 
     } 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, 
                 for: indexPath) as! ImageCollectionViewCell 
     cell.imageView.image = self.images[indexPath.row] 

     return cell 
    } 
0

應該返回1在numberOfSection FUNC

func numberOfSections(in collectionView: UICollectionView) -> Int { 
return 2 

}