0

For some strange reason the collectionView isn't called. I put break points at numberOfItemsInSection and cellForItemAtIndexPath but they are never called. Here is my code:

class ChannelViewController: UIViewController, UISearchResultsUpdating, UICollectionViewDataSource, UICollectionViewDelegate {

  var channelArray: [ChannelInfo] = []
  var filteredSearchResults = [ChannelInfo]()
  var resultsSearchController = UISearchController(searchResultsController: nil)
  var logosShown = [Bool](count: 50, repeatedValue: false)
  var detailUrl: String?
  var apiKey = ""
  var channel: String!
  var channelForShow: String!
  var task: NSURLSessionTask?

  @IBOutlet var channelCollectionView: UICollectionView!

  override func viewDidLoad() {

    let baseURL = ""
    getJSON(baseURL)
  }


  //MARK: CollectionView

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

    if resultsSearchController.active && resultsSearchController.searchBar.text != ""
    {
      return filteredSearchResults.count
    } else {
      return channelArray.count
    }
  }


 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ChannelCell", forIndexPath: indexPath) as! ChannelCell
    let channel: String
    if resultsSearchController.active && resultsSearchController.searchBar.text != "" {
      channel = self.filteredSearchResults[indexPath.row].logo
    } else {
      channel = self.channelArray[indexPath.row].logo
    }
    return cell
  }

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    var replacedTitle: String?
    if resultsSearchController.active && resultsSearchController.searchBar.text != "" {

      channelForShow = filteredSearchResults[indexPath.row].channelName

      }
    } else {
      channelForShow = self.channelArray[indexPath.row].channelName
      }
    }
    self.dismissSearchBar()
    performSegueWithIdentifier("channelCollectToShowSegue", sender: self)
  }

}

I also have a cell subclassed for the collectionView Cell:

class ChannelCell : UICollectionViewCell {

  @IBOutlet var channelImageView: UIImageView!

}
tylerD
  • 53
  • 10

2 Answers2

1

You need to set the datasource of collection view to the controller.

1) In Interface builder, just drag from the collection view to the controller. and choose both the delegate and datasource..

enter image description here enter image description here

2) Programatically

override func viewDidLoad() {
    super.viewDidLoad()

    channelCollectionView.delegate = self
    channelCollectionView.dataSource = self

   let baseURL = ""
    getJSON(baseURL)
  }
Earl Grey
  • 7,426
  • 6
  • 39
  • 59
0

Did you set the collectionView's delegate and dataSource properties? If not, change your viewDidLoad to this, in order to set those two properties programmatically:

override func viewDidLoad() {
    channelCollectionView.delegate = self
    channelCollectionView.dataSource = self
    let baseURL = ""
    getJSON(baseURL)
}

If you don't set the delegate and dataSource, the collectionView won't know where to look to call the appropriate methods, such as cellForItemAtIndexPath. Hopefully this fixes your problem.

BradzTech
  • 2,755
  • 1
  • 16
  • 21