Collection View not showing up

I'm currently making a tabbed app, and im having some troubles getting some things to show up during the test runs. How do you get the collection view that was made in a storyboard to show up when running the test on a device? everything is configured porperly in the storyboard, and other things show up in other tabs, like the navigation bars and such, but for some reason, i can't get the collection view to appear

  • Select the UICollectionView in the View Controller Scene menu (middle of Xcode, but left of Main Storyboard visual), and then in Size Inspector change the "Estimated Size" from AUTOMATIC to "NONE". If you have all your hookups done correctly, and constraints are correct in your main Storyboard scene, you should see approximately what you designed instead of small cells.

Add a Comment

Replies

Is it this part of code ?


class SecondViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 
 
    func numberOfSections(in collectionView: UICollectionView) -> Int { 
        return 1 
    } 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
        return 1 
    } 
     
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Movie1",  for: indexPath) 
        return cell 
    } 
    
    override func viewDidLoad() { 
        super.viewDidLoad() 
        // Do any additional setup after loading the view. 
    } 
     
}

If so, I do not see where you set the image in the cell.

so thats my SecondViewController code, the image is connected to the FirstCollectionViewCell code, which is attached to the first cell:

import UIKit

class FirstCollectionViewCell: UICollectionViewCell {
    
   
    @IBOutlet weak var Movie1: UIImageView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }
    
}

UPDATE! I wasw able to get the cell sizes to show correctly. Now my only trouble is getting all of the different images/cells to show up in their specified spots

If SecondViewController is the one controlling the collection of FirstCollectionViewCell, then,


in SecondViewController, change as follows:


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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Movie1",  for: indexPath)  as! FirstCollectionViewCell
        let imageView = UIImageView(image: UIImage(named: "The right Image"))     // Image depends on indexPath.row
        cell.contentView.insertSubview(imageView, at: 0)

        return cell
    }

or


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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Movie1",  for: indexPath)  as! FirstCollectionViewCell
        let imageView = UIImageView(image: UIImage(named: "The right Image"))     // Image depends on indexPath.row
        cell.Movie1.insertSubview(imageView, at: 0)

        return cell
    }

Note that Movie1 property name should start with lowercase movie1

So the image is showing perfectly with the code i have, my issue is that it is showing the same image in every cell on the tests, instead of the different image per cell, as configured in the IB. so i guess my problem would be getting cells 2, 3, 4, 5, and 6 to show their images instead of all the cells have the image of cell 1. Also, i did change the code to the one you suggested and it is NOT what im wanting lol, It takes away the cell background color

Where are the images 1, 2, 3… 6. Are they stored in an array ?


Try this variation


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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Movie1",  for: indexPath)  as! FirstCollectionViewCell
        let imageView = UIImageView(image: UIImage(named: "The right Image"))     // Image depends on indexPath.row
        cell.Movie1 = imageView

        return cell
    }

That displays the same thing that i already have. Is it possible for me to drop the public link to test flight the app so you can see what it looks like?

If you want the cell to display a specific image, you need to define which image in cellForItemAt

And need to use the proper class for the cell


class FirstCollectionViewCell: UICollectionViewCell {
   
    @IBOutlet weak var Movie1: UIImageView!     // should probably call it movie, not Movie1
   
    override func awakeFromNib() {
        super.awakeFromNib()
    }
   
}


  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Movie1", for: indexPath)  as! FirstCollectionViewCell
         cell.movie.image = UIImage(named: "some image") // set the right image
         return cell
     }