bigdogg99juherd
bigdogg99juherd

Reputation: 27

Data not displaying correctly in CollectionView cell?

I have trying to make a program that will display the image of crystals. After writing the code and formatting the StoryBoard, when I run the app the images are tiny and there are no labels displayed. I have constraints. Screenshot of iPhone11

Here is the code:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    let imageArray = [UIImage(named: "1"),UIImage(named: "2"),UIImage(named: "3")]
    let nameArray = ["Rose Quartz", "Clear Quartz", "Clear Quartz 2"]
    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: "MainCollectionViewCell", for: indexPath) as! MainCollectionViewCell
        
        cell.crystalPhotoImageView.image = imageArray[indexPath.row]
        cell.crystalNameLabel.text! = nameArray[indexPath.row]
        
        return cell
    }
    
}

Upvotes: 0

Views: 98

Answers (1)

Momin J Abusaada
Momin J Abusaada

Reputation: 101

you need to implement to confirm the class has UICollectionViewDelegateFlowLayout and you need to implement this functionn

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return cellSize
}

Upvotes: 1

Related Questions