Это возможно, вам просто нужно добавить каждый UICollectionView в качестве подвью и установить делегат и dataSource на ваш UIViewController.
Вот краткий пример. Если у вас есть один UICollectionView работы, вы должны быть в состоянии адаптировать этот код для собственных целей, чтобы добавить вторую довольно легко:
let collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()
let collectionViewAIdentifier = "CollectionViewACell"
let collectionViewBIdentifier = "CollectionViewBCell"
override func viewDidLoad() {
// Initialize the collection views, set the desired frames
collectionViewA.delegate = self
collectionViewB.delegate = self
collectionViewA.dataSource = self
collectionViewB.dataSource = self
self.view.addSubview(collectionViewA)
self.view.addSubview(collectionViewB)
}
В функции cellForItemAtIndexPath делегата:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewA {
let cellA = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier) as UICollectionViewCell
// Set up cell
return cellA
}
else {
let cellB = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier) as UICollectionViewCell
// ...Set up cell
return cellB
}
}
В функции numberOfItemsInSection :
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.collectionViewA {
return 0 // Replace with count of your data for collectionViewA
}
return 0 // Replace with count of your data for collectionViewB
}
Я получаю эту ошибку: «UICollectionView должен быть инициализирован параметром макета не-nil» в этой строке «let collectionViewA = UICollectionView()» – dennis
это не сработало для меня! Я получил эту ошибку: *** Завершение приложения из-за неперехваченного исключения «NSInvalidArgumentException», причина: «UICollectionView должен быть инициализирован параметром макета не-nil», –