Reputation: 27
I am trying to display a collection of horizontally scrollable images with the help of the UICollectionView . I did a custom view controller file for the cell but i still don`t get any images displayed. This is my code in Viewcontroller:
class ViewController: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource{
var imageArray = [UIImage(named:"1"),UIImage(named:"2"),UIImage(named:"3"),UIImage(named:"4"),UIImage(named:"5"),UIImage(named:"6"),UIImage(named:"7"),UIImage(named:"8"),]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "coffeeImageCollectionViewCell", for: indexPath) as! coffeeImageCollectionViewCell
cell.Imgimage.image = imageArray[indexPath.row]
return cell
}
}
And this is in my custom CollectionViewCell class, to which i linked the image view:
import UIKit
class coffeeImageCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var Imgimage: UIImageView!
}
Upvotes: 0
Views: 55
Reputation: 27
I solved it. The tutorial i used is from 2 years ago and the person is using version 3. I just changed the "!" after "as" to "?" and added a "?" after cell (.image). Also, i added a "!" after return cell to unwrap
Upvotes: 0
Reputation: 100503
You need to set in viewDidLoad
self.collectionView.delegate = self
self.collectionView.dataSource = self
With creating this outlet
@IBOutlet weak var collectionView: UICollectionView!
Upvotes: 1