22 Replies
      Latest reply on Jul 27, 2019 7:01 AM by Claude31
      Meiscollinj Level 1 Level 1 (0 points)

        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

        • Re: Collection View not showing up
          Claude31 Level 8 Level 8 (6,595 points)

          Have you defined the delegate and datasource for the collectionView ? That's the most likely cause for not showing any content of collectionView.

           

          However, the collectionView background should show.

          You should set the color of collectionView to check that you see it.

           

          If not, you may have constrai,nts thjat are not correct.

            • Re: Collection View not showing up
              Meiscollinj Level 1 Level 1 (0 points)

              What should i be defining the delegate and datasource as? Everytime i set the delegate and datasource to the view controller, and i run the test, the moment i tap on that tab with the collectionview, it crashes the app and xcode gives me an error

                • Re: Collection View not showing up
                  Claude31 Level 8 Level 8 (6,595 points)

                  Which error do you get ?

                   

                  How do you set the delegates: in IB ? in code ?

                  The simplest is to set them in IB, so you are sure to connect to the right controller.

                   

                  Have you declared your viewController as conforming to collectionView protocols ?

                   

                  classMyViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

                    • Re: Collection View not showing up
                      Meiscollinj Level 1 Level 1 (0 points)

                      when i connect the datasource and delegate through the IB, and tap on the tab, i get the error "libc++abi.dylib: terminating with uncaught exception of type NSException" and then it opens the AppDelegate.swift and highlights the first line with the error "Thread 1: signal SIGABRT." and if i declare the collectionview protocols, i am clueless what to put in the code for the indexpath...if i just do the delegate through the IB, and run the app, i have no issues, its specifically the datasource connection that makes the app crash.

                        • Re: Collection View not showing up
                          Claude31 Level 8 Level 8 (6,595 points)

                          In some cases, dataSource is implicitely defined (that's why it may work even though you did not connect).

                           

                          when i connect the datasource and delegate through the IB,

                           

                          Please detail where are (in which classes)

                          - collectionView

                          - the controller you connect for dataSource and delegate.

                           

                          Please, also post the complete code for the controller that includes the collectionView

                           

                          Note: the fact that crash appears in AppDelegate isdoes not mean error occurs here.

                            • Re: Collection View not showing up
                              Meiscollinj Level 1 Level 1 (0 points)

                              Heres the code for the second view controller:

                              import UIKit
                              
                              class SecondViewController: UIViewController {
                              
                                   override func viewDidLoad() {
                                        super.viewDidLoad()
                                   
                                   }
                              
                              }

                              i have on the actual scene in the IB, the collection view, the collection view cells, and a segmented control. The segmented control shows up in the tests and i have no issues with it. and when i test the collection view by changing the background color, it shows up. It's just the cells that i cant get to show up.

                               

                              When i connect the datasource and delegate, im connecting the collection view to the scene that it's in, which is the SecondViewController. It is specifically the datasource connection that causes problems with the app when i tap on the tab.

                                • Re: Collection View not showing up
                                  Claude31 Level 8 Level 8 (6,595 points)

                                  Sorry, but that code shows nothing useful to understand what's going on.

                                   

                                  i have on the actual scene in the IB, the collection view, the collection view cells, and a segmented control.

                                  Are they all in one controller. Is it  SecondViewController ? Or another one ?

                                   

                                  The segmented control shows up in the tests and i have no issues with it.

                                  and when i test the collection view by changing the background color, it shows up.

                                   

                                  Where do you see ? In IB or in the running app ?

                                   

                                  It's just the cells that i cant get to show up.

                                  You cannot show them when running the app or in IB ?

                                   

                                  What is needed is the code of the viewController that includes the collectionView.

                                   

                                  If you cannot show the full code (not just viewDidLoad), sorry, impossible to help you.

                                    • Re: Collection View not showing up
                                      Meiscollinj Level 1 Level 1 (0 points)

                                      They are all in one controller. Is it  SecondViewController ? Or another one ?

                                      Everything is in the SecondViewController.

                                       

                                      Where do you see ? In IB or in the running app ?

                                      When i run the test, i run it through my iPhone X, and thats where the segmented control shows up; on both the storyboard and within the test run of the actual app.

                                       

                                      You cannot show them when running the app or in IB ?

                                      The collection view cells show up in the IB, but not when running the app.

                                       

                                      If you cannot show the full code (not just viewDidLoad), sorry, impossible to help you.

                                      That code i wrote is all of the secondviewcontroller code. Theres nothing else written in there. I am unsure of what to write to get the cells to show up. Everything else is showing up without me having to add any extra code into the SecondViewController

                                        • Re: Collection View not showing up
                                          Claude31 Level 8 Level 8 (6,595 points)

                                          So, to see the cells, you need to declare that class conforms to collectionView delegate protocols and write the collectionView delegate methods. Should be something like this:

                                           

                                          You should define a class (FirstCollectionViewCell) for the collectionViewCells, with its xib (create a new file, select it as subclass ou UICollectionViewCell and create the xib at the same time)

                                           

                                          import UIKit
                                          
                                          class FirstCollectionViewCell: UICollectionViewCell {
                                            
                                              @IBOutlet weak var label: UILabel!
                                            
                                              override func awakeFromNib() {
                                                
                                                  super.awakeFromNib()
                                                  // Initialization code
                                              }
                                            
                                          }

                                           

                                          In IB, select the CollectionView, select the cell and hive it the class FirstCollectionViewCell

                                           

                                          Create a label in its xib that you connect to the IBOutlet

                                           

                                           

                                          import UIKit
                                          
                                          class SecondViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
                                          
                                               var items : String = []          // Just to have a sample data
                                          
                                              @IBOutlet weak var collectionView: UICollectionView!     // connect to the CollectionView in IB ; you may need it later
                                          
                                               override func viewDidLoad() {
                                                    super.viewDidLoad()
                                                    // Let's create the dataSource example
                                                    for i in 1...10 {
                                                         let text = "MyCell n° \(i)"
                                                    }
                                                   collectionView.delegate = self // Unless you have already defined the delegate in IB
                                                   collectionView.dataSource = self // Unless you have already defined the dataSource in IB
                                               }
                                          
                                               // Here are the delegate and dataSource methods
                                          
                                               // If you have only onse section, you can omit this one, but it is good practice to include
                                              func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
                                                  return 1
                                              }
                                          
                                              func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                          
                                                  return items.count  // howMany cells do you have in collection ? that is items.count, where items is the array containing the cells data
                                              }
                                          
                                          // Here, you populate the collection from the dataSource
                                          // You need to have defined a cellIdentitifier in IB : I call it CollectionCellID
                                              func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                          
                                                  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellID", for: indexPath) as! FirstCollectionViewCell
                                                  cell.label.text = items[indexPath.row]
                                                  return cell
                                              }
                                          
                                          }
                                          
                                          

                                          Hope I did not forget anything

                                            • Re: Collection View not showing up
                                              Meiscollinj Level 1 Level 1 (0 points)

                                              im receiving numerous errors after typing in the code, specifically in the lines:

                                               

                                              var items : String = []

                                              "cannot convert valueof type '[any]' to specified type 'String'

                                               

                                              collectionView.delegate = self
                                              collectionView.dataSource = self

                                              both receive the error: Ambiguous reference to member 'collectionView(_:numberOfItemsInSection:)'

                                               

                                              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCellID", for: indexPath) as! FirstCollectionViewCell
                                              cell.label.text = items[indexPath.row]

                                              and for those 2 lines it's the error: subscript(_:)' is unavailable: cannot subscript String with an Int, see the documentation comment for discussion

                                                • Re: Collection View not showing up
                                                  Claude31 Level 8 Level 8 (6,595 points)

                                                  var items : String = []

                                                   

                                                  of course, should be

                                                   

                                                  var items : [String] = []

                                                   

                                                  And just remove

                                                   

                                                     func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

                                                          return 1

                                                      }

                                                    • Re: Collection View not showing up
                                                      Claude31 Level 8 Level 8 (6,595 points)

                                                      Can you post the exact code of your SecondViewController, and report any error after the modfication.

                                                        • Re: Collection View not showing up
                                                          Meiscollinj Level 1 Level 1 (0 points)

                                                          alright, after waiting for Apple to release an update so i could actually open my app, i finally got something to show up from the collection view. The only problem that im having is the size of the cell, and im unsure of how to get the other cells to show up. here's my working code:

                                                          import UIKit
                                                          
                                                          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.
                                                              }
                                                              
                                                          }
                                                          
                                                          
                                                            • Re: Collection View not showing up
                                                              Claude31 Level 8 Level 8 (6,595 points)

                                                              Did you define the cell size in IB or in code ?

                                                                • Re: Collection View not showing up
                                                                  Meiscollinj Level 1 Level 1 (0 points)

                                                                  IB. but i also have 6 cells, each with a different image in it. its showing 6 cells in 2 columns, exactly like i want it to, but its all the same cell, just repeated, and very small

                                                                    • Re: Collection View not showing up
                                                                      Claude31 Level 8 Level 8 (6,595 points)

                                                                      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.

                                                                        • Re: Collection View not showing up
                                                                          Meiscollinj Level 1 Level 1 (0 points)

                                                                          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

                                                                            • Re: Collection View not showing up
                                                                              Claude31 Level 8 Level 8 (6,595 points)

                                                                              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

                                                                                • Re: Collection View not showing up
                                                                                  Meiscollinj Level 1 Level 1 (0 points)

                                                                                  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

                                                                                    • Re: Collection View not showing up
                                                                                      Claude31 Level 8 Level 8 (6,595 points)

                                                                                      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
                                                                                          }
                                                                                        • Re: Collection View not showing up
                                                                                          Meiscollinj Level 1 Level 1 (0 points)

                                                                                          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?

                                                                                            • Re: Collection View not showing up
                                                                                              Claude31 Level 8 Level 8 (6,595 points)

                                                                                              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
                                                                                                   }