'NSInvalidArgumentException',原因:' - [ViewController tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例错误

时间:2016-03-08 16:04:12

标签: ios swift uicollectionview uisearchbar uisearchdisplaycontroller

按下搜索栏时,我不断收到此错误消息。 collectionView嵌入在viewcontroller中,搜索栏位于collectionView:

之外

CollectionView inside ViewController

collectionView的数据源和委托连接到它们嵌入的viewController。奇怪的是,我甚至没有tableView,而是一个collectionView。

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!

  @IBOutlet var channelCollectionView: UICollectionView!
  @IBOutlet var channelSearchBar: UISearchBar!

  override func viewDidLoad() {
    SearchBarAttributes()
    }


  //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.item].logo
    } else {
      channel = self.channelArray[indexPath.item].logo
    }

    return cell
  }

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

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

      channelForShow = filteredSearchResults[indexPath.item].channelName

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

 //MARK: Search

  func SearchBarAttributes () {

    self.resultsSearchController = UISearchController(searchResultsController: nil)//required!
    self.resultsSearchController.searchResultsUpdater = self// required!
    self.resultsSearchController.definesPresentationContext = true//removes search controller if move to other vc
      channelSearchBar = self.resultsSearchController.searchBar
    self.channelCollectionView.reloadData()
  }

  func updateSearchResultsForSearchController(searchController: UISearchController) {

    self.filteredSearchResults.removeAll(keepCapacity: false)
    let searchPredicate = NSPredicate(format: "channelName CONTAINS [c] %@", searchController.searchBar.text!)
    let array = (self.channelArray as NSArray).filteredArrayUsingPredicate(searchPredicate)
    self.filteredSearchResults = array as! [ChannelInfo]
    self.channelCollectionView.reloadData()
  }
}

以下是带有Searchbar IB连接的故事板:

Storyboard with IB Connections

0 个答案:

没有答案