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

Viewed 6714

I am using swift 3 - I know how to show some Images in collection view with custom Cell - the problem is that I can't use custom cell in Collection View Controller

here is the collection view Controller Codes 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 view Cell Code

   import UIKit

  class UICollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var cellImages: UIImageView!
   }

remember that I used "uploadCell" for identifier

2 Answers

UICollectionView implementation is quite interesting. You can quite simple source code and video tutorial from these link :

https://github.com/Ady901/Demo02CollectionView.git

https://www.youtube.com/watch?v=5SrgvZF67Yw

class DummyCollectionCell: UICollectionViewCell {

    @IBOutlet weak var userImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DummyCollectionCell", for: indexPath) as! DummyCollectionCell
                cell.titleLabel.text = nameArr[indexPath.row]
                cell.userImageView.backgroundColor = .blue
                return cell
}
Related