How to use custom cell in collection view controller in swift 3

I am using swift 3 - I know how to show some images in a collection view with a custom cell - the problem is I cannot use the custom cell in the Collection View Controller

here are the codes of the collection controllers private let reuseIdentifier = "uploadCell"

class uploadedFiles: UICollectionViewController {

var images = ["1.jpg" , "2.jpg" , "3.jpg" , "4.jpg"  ]

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
    //myCollectionView.sizeToFit()
    //cell.sizeToFit()


    cell.cellImages.image = UIImage(named: images[indexPath.row])



    return cell
}

      

and here is the collection cell code

   import UIKit

  class UICollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var cellImages: UIImageView!
   }

      

remember i used "uploadCell" for id

+3


source to share


1 answer


To use Custom Cell CellView

Step 1. Subclassing CollectionViewCell

class ImageCell: UICollectionViewCell {
    @IBOutlet weak var imgView: UIImageView!
}

      



Step 2: set the CollectionViewCell class to StoryBoard

Step 3: reuseCollectionView Cell

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell

    cell.imgView.image = self.arrMedia[indexPath.row] as? UIImage
    return cell
}

      

+5


source







All Articles