Joel J.
Joel J.

Reputation: 425

CollectionView Cells Not showing

I'm trying to show images in a UICollectionView. The code below is responsible for showing the cell.

    super.viewWillAppear(animated)
    memes = appDelegate.memes
    collectionView?.reloadData()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    let space:CGFloat = 3.0
    let dimension = (view.frame.size.width - (2 * space)) / 3.0

    flowLayout.minimumInteritemSpacing = space
    flowLayout.minimumLineSpacing = space
    flowLayout.itemSize = CGSize(width: dimension, height: dimension)
    flowLayout.sectionInset = UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0)
}

and

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MemeCollectionViewCell", for: indexPath) as! MemeCollectionViewCell
    let meme = memes[(indexPath as NSIndexPath).row]

    cell.memeImageView?.image = meme.meme
    // Configure the cell

    return cell
}

All I see is a blank view controller. When I click the screen, the details VC popups up (as it should). It seems like the information for the view is there but just isn't being rendered on the screen. What changes do I need to make in order to ensure the images show?

Upvotes: 1

Views: 8726

Answers (4)

Asad Jamil
Asad Jamil

Reputation: 198

I faced the same issue, when I debugged the issue it was that collectionView is not getting height to be displayed and delegate method cellForItem was not even working. Solution: I set me leading and trailing constraint to the parentView outside stackView. My view hierarchy was parentView->stackView->collectionView. Comment if you have any question.

Upvotes: 0

Joel J.
Joel J.

Reputation: 425

it turns out I didn't have the module name set in the collection view controller in storyboard.

Upvotes: 1

iOS Geek
iOS Geek

Reputation: 4855

1) use default method to provide flowLayout to a collectionView

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

        return CGSize(width: width/3, height: 100)
    }

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets{

        return  UIEdgeInsetsMake(0, 0, 0, 0)
    }

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{

        return 0
    }

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{

        return 0
    }

2) make sure you had initialised in code before you reload your collectionViw

collectionview.delegate = self
 collectionview.dataSource = self

3) add breakpoints in numberOfItemsInSectionafter relaoding and check do your meme array really have value the you reload CollectionView and if yes , of course it will after this go to CellforRowAt and check do your memeImageView is getting value or not . print image view using breakpoint just for try

Upvotes: 0

anamika41
anamika41

Reputation: 161

Your collectionview delegates are not being called. you have to write

collectionview.delegate = self collectionview.dataSource = self

And if the delegates are being called, please check if meme.meme has and image.

Upvotes: 1

Related Questions