我有这个代码在另一个ViewController中创建一个UICollectionView:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tvSeries.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let identifier = "Item"
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! SeriesCollectionViewCell
cell.itemLabel.text = tvSeries[indexPath.row]
cell.itemImage.image = UIImage.init(imageLiteralResourceName: tvSeries[indexPath.row])
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let item = sender as? UICollectionViewCell
let indexPath = collectionView.indexPath(for: item!)
let detailVC = segue.destination as! DetailViewController
detailVC.detailName = tvSeries[(indexPath?.row)!]
}
我想要一个带有2个CollectionViews的ViewController,如下图所示:
答案 0 :(得分:2)
在诸如numberOfItemsInSection
之类的collectionView函数中,检查collectionView
参数是否等于一个或另一个集合视图,然后根据需要返回该数字。
例如,如果您有以下IBOutlets:
@IBOutlet weak var collectionViewOne: UICollectionView!
@IBOutlet weak var collectionViewTwo: UICollectionView!
然后您的numberOfItemsInSection
将更改为:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == collectionViewOne {
return itemSource1.count
}
else {
return itemSource2.count
}
}
对其余的collectionView函数执行相同的操作。