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
}
我做错了什么?我错过了什么?
请参考https://stackoverflow.com/a/39919303/4611751 – Rajesh73
我提到了这个链接的解决方案,但不幸的是,这似乎并没有解决我的问题。 – 209135
发布您的代码,重新载入collectionview – Rajesh73