2017-07-19 52 views
0

我有一個UICollectionView嵌套在另一個ParentCollectionView,問題是當我設置數據到它的數據變得混亂。部分標題按順序設置,但嵌套的集合視圖不按順序。 下面的代碼Swift CollectionView排序障礙

import UIKit 


class HomeView: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { 
    @IBOutlet weak var parentCollectionView: UICollectionView! 

    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint! 

    let viewModel = HomeVM() 
    var categories = [ServiceSection]() 
    var categoriesReversed = [[String:Any]]() 
    var collectionViewTags = Int() 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.categories = [ServiceSection(
      section: 
       ["id":"2","name":"planet","service":[ 
       ["id":"1","name":"Sun"] 
        ] 
      ] 
      ), 
      ServiceSection(
          section: 
          ["id":"2","name":"Animal","service":[ 
           ["id":"1","name":"Dog"] 
           ] 
          ]) 
     , 

      ServiceSection(
       section: 
       ["id":"2","name":"Daries","service":[ 
       ["id":"1","name":"Milk"] 
        ] 
       ]) 
     , 

      ServiceSection(
       section: 
       ["id":"2","name":"Meet","service":[ 
        ["id":"1","name":"Steak"] 
        ] 
       ]) 

     ] 


     let height = (self.parentCollectionView.collectionViewLayout.collectionViewContentSize.height) 

     self.collectionViewHeight.constant = height 
    } 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     if (collectionView == parentCollectionView) 
     { 
      return self.categories.count 
     } 
     else 
     { 
      collectionView.tag = collectionViewTags 
      collectionViewTags += 1 
      return 1 
     } 
    } 

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

     if (collectionView == parentCollectionView){ 
      return 1 
     }else{ 
      let section = self.categories[collectionView.tag].services 
      return section.count 

     } 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 


     if (collectionView == parentCollectionView){ 
      return collectionView.dequeueReusableCell(withReuseIdentifier: "parentCell", for: indexPath) 
     } 
     else 
     { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionCell 


      let collection = self.categories[collectionView.tag].services 
      let cellData = collection[indexPath.row] 
      cell.title.text = cellData.name 

      return cell 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, 
         layout collectionViewLayout: UICollectionViewLayout, 
         sizeForItemAt indexPath: IndexPath) -> CGSize { 

     if collectionView == parentCollectionView{ 

      return CGSize(width: parentCollectionView.frame.width, height: 30) 

     } 
     return CGSize(width:150,height:80) 

    } 

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 

     if parentCollectionView == collectionView { 

      let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! CollectionHeader 
      header.header.text = self.categories[indexPath.section].name 
      return header 
     } 

     return UICollectionReusableView() 
    } 


} 
+0

您需要按什麼命令? –

+0

按照它們在數據源中的順序排序。請查看章節標題和章節內容,並將它們與它們不匹配的數據源進行比較 – moawaya

回答

0

解決,

說明:問題是我設置的CollectionView的高度在故事板爲「100」,因爲我將它設置動態後來反正(在的CollectionView後負載),但是這會導致使用reloadData()時無法正確加載視圖的非常有線的行爲我猜測操作系統試圖做的只是關心屏幕上可見的部分。

第二個問題是,由於某種原因(我不知道),我不得不重新加載使用numberofsection作爲參數,我從以前稱爲reloadData的部分,我不知道爲什麼我需要重新加載每個部分(我很想投票支持某人解釋它)

只需:collectionView取決於高度和高度取決於集合視圖。

解決方案:我不知道,如果是做正確的方式,但它爲我工作,我改變了的CollectionView高度爲9000或任何大的號碼,以便操作系統知道它需要加載整個事情並致電reloadData()糾正高度。

self.parentCollectionView.reloadData() 


     let range = Range(uncheckedBounds: (0, self.parentCollectionView.numberOfSections)) 
     let indexSet = IndexSet(integersIn: range) 
     self.parentCollectionView.reloadSections(indexSet) 

     let height = (self.parentCollectionView.collectionViewLayout.collectionViewContentSize.height) 
     self.collectionViewHeight.constant = height