UICollectionView and UITableView to work in UIViewController

Hi,

I am trying to get a UICollectionView and a UITableView to work inside a UIViewController. Firstly what I am trying to do is get the UICollectionView to display 25 cells with labels inside, with set text from an array. I have been able to get a UICollectionView to display 25 images in 25 cells in a seperate project, however I think I am missing something when trying to recreate this in my new project. I think I am not creating the delogate and the datasource correctly because the UICollectionView is not a UICollectionViewController.


Any help would be great. Thank you.

It's impossible to help with the amount of information you've given. If you're having problems with code, post the code that reproduces the problem.


Your VC doesn't have to be a UICollectionViewController (or UITableViewController). I almost never use those myself. It can be a plain UIViewController subclass and just has to conform to the data source and delegate protocols. And as you mentioned, yes of course you have to set the VC as the table / collection view's delegate and data source. You can do this in IB (right click on table or collection view; drag from the '+' by the dataSource and delegate outlets to your VC) or in code (e.g. self.myTableViewAssumingThatIsWhatINamedTheOutlet.delegate = self).

Sorry, I have a UIViewController as the superclass and the UICollectionView (not UICollectionViewController) as a subclass and a UITableView (not UITableViewController) as another subclass.


I have created the prototype cell in the UICollectionView with a UILabel inside. Custom class for UICollectionView is set to BtnCollectionView and the custom class for the Cell is set to BtnCollectionViewCell.


BtnCollectionViewCell.swift

import UIKit

class BtnCollectionViewCell: UICollectionViewCell {
     @IBOutlect var btnLabel:UILabel!
}


BtnCollectionView.swift

import UIKit

private let reuseIdentifier = "Cell"

class BtnCollectionView: UICollectionView {
     var BtnLabels = [...] // Array of 25 text strings

     func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
          return 1
     }

     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return BtnLabels.count
     }

     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)

          // ERROR - "Value of type UICollectionViewCell has no member btnLabel"
          cell.btnLabel.text = UILabel()
     }


This is all basic code but the last line of code in the BtnCollectionView does not recognise the btnLabel. I'm not sure why? I do have another class of FirstViewController, this is the class for the superclass (UIViewController) in the storyboard. Does this make any more sense.

Some of it makes sense 😀


The error you're seeing is because the dequeueReusableCell... method returns a UICollectionViewCell. You have to cast it to your subclass by adding "as! BtnCollectionViewCell" at the end. Of course this would crash at runtime if the cell is not an instance of that class, but if everything is set up correctly that will never happen.


You might have some other issues to fix such as trying to assign a UILabel object to a property of type String but one thing at a time 🙂

Thank you for your help. However I have had to move away from using the UICollectionView, and I have switched to using 25 buttons instead.

UICollectionView and UITableView to work in UIViewController
 
 
Q