Ler Ws Ler Ws - 1 month ago 22
Swift Question

How to add three or more collectionViews in any viewController

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if collectionView == thisSeasonCollectionView {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell

let imageView = cell.viewWithTag(1) as! UIImageView

let url = NSURL(string: URLArrayStringThisSeason[indexPath.row])

let placeholderImage = UIImage(named: "Rectangle")!

let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: imageView.frame.size,
radius: 0
)

imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
)
cell.layer.cornerRadius = 3.0

return cell
}
else if collectionView == whatsNewCollectionView {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell

let imageView = cell.viewWithTag(1) as! UIImageView

let url = NSURL(string: URLArrayStringRecents[indexPath.row])

let placeholderImage = UIImage(named: "Rectangle")!

let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: imageView.frame.size,
radius: 0
)

imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
)

cell.layer.cornerRadius = 3.0

return cell
}
}


Why doesn't this work? I wish to try to link 3 collection view each from a different tableView Cell to this swift file but it seems like there can only be two. The code works fine if I replace 'else if' with 'else' for some reason.

Edit:

how do I edit the return of cell count:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

if collectionView == thisSeasonCollectionView {
return URLArrayStringThisSeason.count
}else if collectionView == whatsNewCollectionView {
return URLArrayStringRecents.count
}else if collectionView == labelCollectionView {
return URLArrayStringLabel.count
}

}

Answer

Change your code to the following:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as UICollectionViewCell

    if collectionView == thisSeasonCollectionView {
        let imageView = cell.viewWithTag(1) as! UIImageView

        let url = NSURL(string: URLArrayStringThisSeason[indexPath.row])

        let placeholderImage = UIImage(named: "Rectangle")!

        let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
            size: imageView.frame.size,
            radius: 0
        )

        imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
        )
        cell.layer.cornerRadius = 3.0
    }
    else if collectionView == whatsNewCollectionView {
        let imageView = cell.viewWithTag(1) as! UIImageView

        let url = NSURL(string: URLArrayStringRecents[indexPath.row])

        let placeholderImage = UIImage(named: "Rectangle")!

        let filter = AspectScaledToFillSizeWithRoundedCornersFilter(
            size: imageView.frame.size,
            radius: 0
        )

        imageView.af_setImage(withURL: url as! URL, placeholderImage: placeholderImage, filter: filter, imageTransition: .crossDissolve(0.2)
        )

        cell.layer.cornerRadius = 3.0
    }

    return cell
 }

Make the return cell at the end since you will only hit one of your statements and all statements needs to return a cell, so no need to return cell on three different places, it´s enough to place it in the end. And it´s not necessary to add the let cell = collectionView.dequeu... two times, it´s enough with one time.

Comments