2016-08-03 67 views
0

重裝的問題這是refrence鏈接:多在UICollectionView在最後一個單元格UICollectionView自定義佈局斯威夫特

https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest

我已經實現負載中的數據被下載,下載後完成,我想重裝收集

collectionView.collectionViewLayout.invalidateLayout() 
self.collectionView.reloadData() 

let concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 
dispatch_async(concurrentQueue) { 
      DataManager.sharedInstance.homeRequest(Dictionary: params, onSuccess: { (dict) in 
       self.downloadPageIndex += 1 
       let response = HomeObject(dictionary: dict) 

       for (_,e) in (response.dataDict?.enumerate())!{ 
        self.dataArray.append(e) 
       } 

       dispatch_async(dispatch_get_main_queue()) { 

        self.collectionView.reloadData() 
        self.collectionView.collectionViewLayout.invalidateLayout() 

       } 
       self.activityIndicatorCell.stopAnimating() 

       }, onError: { (error) in 
        self.activityIndicatorCell.stopAnimating() 

      }) 

這樣的CollectionView reloadata

任何一個能幫助我嗎?

+0

這些行有什麼問題?你的問題是什麼? – Larme

+0

你可以顯示更多的負載?當你收到數據時發生了什麼? – kiera91

+0

Collectionview不重載.... – Ravi

回答

0

進入你的問題,目前面臨同樣的問題,經過一些挖掘我想通了!

的問題是PintrestLayout文件

內裏面你會發現像(問題)代碼:

guard cache.isEmpty == true, let collectionView = collectionView else { return }

基本上,如果你申請到的CollectionView的屬性是空的(他們在開始時),將PintrestLayout應用到它,這是完美的! 但是如果您正在重新加載數據並添加到collectionView中,則需要再次應用佈局,但是當您嘗試再次將佈局應用於collectionView時緩存不是空的,因此它返回並且根本不起作用...

SOLUTION

替換代碼:

guard cache.isEmpty == true || cache.isEmpty == false, let collectionView = collectionView else { 
     return 
    } 

現在你說:「我不關心,如果它爲空或不,只是採用了該死的佈局,我的CollectionView」

完成!現在reloadData()會更新你有多少個單元...

雖然這很貴,但是如果你想讓速度更快,並修剪一些內存等......然後看看invalidateFlowLayout文檔。

+0

這也適用於將來面臨這個問題的任何人! – amirkrd