2017-09-01 9 views
1

同様の質問hereがあるようですが、私の問題は解決しませんでした。次のように異なる数のセルを持つviewControllerでの複数のコレクションビュー

私のViewControllerで2 collectionViewsを作成:

func numberOfSections(in collectionView: UICollectionView) -> Int { 
    collectionView.reloadData() 
    collectionView.collectionViewLayout.invalidateLayout() 
    return 1 
} 

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if(collectionView == self.colorCollection){ 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colorsIdentifier", for: indexPath) 
     cell.backgroundColor = .darkGray 
     return cell 
    } else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fontsIdentifier", for: indexPath) 
     cell.backgroundColor = .gray 
     return cell 
    } 
} 

その後、私は2 collectionViewsを切り替えることsegmented controlを使用します。

let y = self.view.frame.height 
titleView = UIView(frame : CGRect(x: 0 , y: 50 , width: self.view.frame.width, height: y - 100)) 

let middle = CGPoint(x: 10, y: titleView.bounds.height/10) 
let frame = CGRect(origin: middle , size: CGSize(width: self.view.frame.width - 20 , height: 70)) 
let layout = UICollectionViewFlowLayout() 
fontCollection = UICollectionView(frame: frame, collectionViewLayout: layout) 
fontCollection.dataSource = self 
fontCollection.delegate = self 
fontCollection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "fontsIdentifier") 
fontCollection.backgroundColor = UIColor.white 
// Do any additional setup after 

colorCollection = UICollectionView(frame: frame, collectionViewLayout: layout) 
//colorCollection.frame.origin.y = fontCollection.frame.origin.y + (2 * fontCollection.frame.height) 
colorCollection.dataSource = self 
colorCollection.delegate = self 
colorCollection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "colorsIdentifier") 
colorCollection.backgroundColor = UIColor.white 
// Do any additional setup after 

は、これらは私のUICollectionViewDataSourceメソッドです。このpoint.However件まで必要に応じて すべては私がcollectionViewのいずれかに異なる数のセルを割り当てたいとき、私はこのエラーを取得する行く:

UICollectionViewはレイアウトが存在しないインデックス パスを持つセルの属性を受信しました{長さ= 2、パス0 = - 6}これは

ある私numberOfItemsInSection方法

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

    if(collectionView == self.colorCollection){ return 9 } 
    else if(collectionView == self.fontCollection){ return 6 } 

    return 0 
} 

何か間違っているのですか?私は何が欠けていますか?

+0

これを参照してくださいhttps://stackoverflow.com/a/39919303/4611751 – Rajesh73

+0

私はこのリンクの解決策に言及しましたが、残念ながらこれは私の問題を解決していないようです。 – 209135

+0

リロードするコードを投稿するcollectionview – Rajesh73

答えて

0

コメントで@ Rajesh73が指摘したように、問題はcollectionViewsの両方に1つのレイアウトを割り当てることにありました。だから私は両方のcollectionViews異なるレイアウトを与え、それは問題を解決しました。

したがって

let fontsLayout =  UICollectionViewFlowLayout() 
    fontCollection =  UICollectionView(frame: frame,collectionViewLayout: fontsLayout) 
    let colorsLayout =  UICollectionViewFlowLayout() 
colorsCollection =  UICollectionView(frame: frame collectionViewLayout: colorsLayout) 

let layout =    UICollectionViewFlowLayout() 
    fontCollection =   UICollectionView(frame: frame, collectionViewLayout: layout) 
    colorCollection =  UICollectionView(frame: frame, collectionViewLayout: layout) 

の交換は、問題を解決しました。

関連する問題