2017-09-01 66 views
1

似乎有类似的问题here但它并没有解决我的问题。在viewController中使用不同数目的单元格的多个集合视图

我创造了我的viewController 2个collectionViews如下:

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方法:

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 
    } 
} 

然后我用一个segmented control两个collectionViews之间切换。 一切按所需的高达这个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。所以我给了这两个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) 

解决的问题。

相关问题