Rorschach Rorschach - 1 year ago 62
Swift Question

UICollection View never loads

I have made a UICollection View. In my ViewController I have the following code:

import UIKit
import AlamofireImage

class MYViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, MYItemCellDelegate {

let viewModel : MYViewModel = StandPlaceViewModel()

@IBOutlet weak var itemCollectionView: UICollectionView!
@IBOutlet weak var ItemCountLabel: UILabel!

override func viewDidLoad() {
self.viewModel.getItems(self.viewModel.stand) { items in
self.ItemCountLabel.text = String(self.viewModel.itemCount)

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.viewModel.tableDict.count

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: MYItemCell = collectionView.dequeueReusableCellWithReuseIdentifier("itemCell", forIndexPath: indexPath) as! MYItemCell
cell.itemTitleLabel.text = self.viewModel.tableDict[indexPath.row]![0]
return cell

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Cell \(indexPath.row) selected")


My Issue is that the UICollection never loads. IN COLLECTION OF ITEMS is never printed. I have a separate page with a collection and it all works just fine and I cant find the difference.

is properly set in the viewDidLoad(), but the reloadData() never seems to call the collectionView code.

I would expect IN COLLECTION OF ITEMS to be printed twice.

Answer Source

Add these code into viewDidLoad

self.itemCollectionView.delegate = self;
self.itemCollectionView.dataSource = self;