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