Rorschach Rorschach - 5 months ago 20
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() {
super.viewDidLoad()
self.viewModel.getItems(self.viewModel.stand) { items in
self.ItemCountLabel.text = String(self.viewModel.itemCount)
self.itemCollectionView.reloadData()
}
}

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


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
print("IN COLLECTION OF ITEMS")
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.

The
ItemCountLabel.text
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

Add these code into viewDidLoad

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