grabury
grabury

Reputation: 5559

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.

Upvotes: 0

Views: 4837

Answers (2)

Lineesh K Mohan
Lineesh K Mohan

Reputation: 1712

SWIFT 3 - In swift 3 the delegate methods are little bit changed. I faced the same issue because of these changes. Please find the correct delegate methods

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    //Do here
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath:IndexPath) -> UICollectionViewCell {
   //Do here
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

  //Do here
}

Upvotes: 0

ad121
ad121

Reputation: 2368

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().

Upvotes: 3

Related Questions