I need to show in a viewController two collectionViews.
At the top a single row and on then on the rest of the view a collectionView with vertical scrolling and two columns.
That's the current piece of code to show both collectionViews:
@IBOutlet weak var collectionViewMarcas: UICollectionView!
var marcas = [Marcas]()
@IBOutlet weak var collectionViewDispositivos: UICollectionView!
var dispositivos = [Dispositivos]()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == collectionViewMarcas {
return marcas.count
}
return dispositivos.count
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// print("MARCA:"+marcas[indexPath.row].nombre)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == self.collectionViewMarcas {
let cellMarcas = collectionView.dequeueReusableCell(withReuseIdentifier: "marca", for: indexPath) as! MarcasCollectionViewCell
let foto_marca = marcas[indexPath.row].icono
print(foto_marca)
let url = URL(string: "https://.../iconos/"+foto_marca)
UIImage.loadFrom(url: url!) { image in
cellMarcas.icono_marca.image = image
}
return cellMarcas
}
else {
let cellDisp = collectionView.dequeueReusableCell(withReuseIdentifier: "dispositivo", for: indexPath) as! DispositivosCollectionViewCell
let foto_dispos = dispositivos[indexPath.row].foto_dispositivo
print(foto_dispos)
let modelo = dispositivos[indexPath.row].modelo_del_dispositivo
cellDisp.modelo_disp.text = modelo
let precio = dispositivos[indexPath.row].precio_dispositivo + " €"
cellDisp.precio_disp.text = precio
let marca = dispositivos[indexPath.row].logo_marca
let urlmarca = URL(string: "https://../iconos/"+marca)
UIImage.loadFrom(url: urlmarca!) { image in
cellDisp.marca_logo.image = image
}
let urlfoto = URL(string: "https://.../dispositivos/"+foto_dispos)
UIImage.loadFrom(url: urlfoto!) { image in
cellDisp.foto_disp.image = image
}
return cellDisp
}
}
Here you have a screenshot:
I have tried implementing UICollectionViewDelegateFlowLayout and then
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 50
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
}
This implementation works fine for the vertical collectionView, but it also changes the horizontal one.
How could I get only the vertical layout changed?
You need To Return both Collection view Size collectionViewLayout delegate method
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
//You only apply the changes to collectionViewMarcas
if collectionView == self.collectionViewMarcas
{
let padding: CGFloat = 50
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
}
// Here Below Line You need To return collectionViewDispositivos cell size
return CGSize(width: 60, height: 60)
}