gpt4 book ai didi

ios - 具有不同单元格数量的 viewController 中的多个 Collection View

转载 作者:可可西里 更新时间:2023-11-01 02:01:29 26 4
gpt4 key购买 nike

好像有个类似的问题here但这并没有解决我的问题。

我在我的 viewController 中创建了两个 collectionView,如下所示:

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

然后我使用一个分段控件在两个collectionViews之间切换。到目前为止,一切都按预期进行。但是,当我想为 collectionView 分配不同数量的单元格时,我收到此错误:

UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 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
}

我做错了什么吗?我错过了什么?

最佳答案

正如 @Rajesh73 在评论中指出的那样,问题在于为两个 collectionViews 分配一个布局。所以我给了两个 collectionViews 不同的布局,它解决了这个问题。

这样替换

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

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

解决了这个问题。

关于ios - 具有不同单元格数量的 viewController 中的多个 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45999443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com