2017-07-19 6 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の高さを設定したが、負荷がかかる)、これは、使用時にビューを正しく読み込まないという有線の動作を引き起こします。私はOSが何をしようとしているかは、画面上に表示されている部分についてのみ気にしていると思います。

2番目の問題は、何らかの理由で(私にはわかりませんでしたが)、前にreloadDataと呼ばれていたパラメータとしてnumberofsectionを使用してリロードセクションを実行する必要がありました。なぜ各セクションをリロードする必要があるのか​​わかりません(私はそれを説明することができる誰かに投票したい)

単純に:コレクションの高さと高さに依存するコレクションビューによって異なります。

ソリューション:私は、それはそれを行うには正しい方法ですが、それは私のために動作するかどうOSは、それが全体をロードする必要が知っているので、私は9000または任意の大きな数にcollectionViewの高さを変更するかわかりません事をして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