grabury grabury - 1 month ago 14
Swift Question

CollectionView datasource and delegate methods not being called

The UICollectionView's datasource and delegate methods are not being called. I have

class MessagesViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.dataSource = self
self.collectionView.delegate = self


And the function

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell


is not being called.

Answer

I can't write a comment because of low reputation, but my guess would be that you set the dataSource and delegate in the viewDidLoad() method. Perhaps hook the dataSource and delegate up in the storyboard by control-dragging from your CollectionView to the ViewController containing it. You could also try reloading the collectionView data with collectionView.reloadData() after you set the dataSource and delegate in viewDidLoad().