1

I have come across over an issue really weird when trying to reload the table for the search string when I run my project

I get an error saying :unexpectedly found nil while unwrapping an Optional value

The code is:

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {

    if tableView == self.searchDisplayController.searchResultsTableView {
        return self.results.count
    }
    else {
      return cells.count

    }
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    var cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("addFriendCell") as UITableViewCell
      var empty = UITableViewCell()
    if cell == empty{
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "addFriendCell")
    }
    if tableView == self.searchDisplayController.searchResultsTableView {
        cell.textLabel.text = self.results[indexPath.row] as NSString

    }
    else{
    cell.textLabel.text = cells[indexPath.row] as NSString
    }
        return cell
}

func filterContentForSearchText(searchText:NSString , scope : NSString){
    var resultPredicate : NSPredicate = NSPredicate(format: "name contains[c]\(searchText)", argumentArray: nil)

    self.results = self.cells.filteredArrayUsingPredicate(resultPredicate)
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
    //self.filterContentForSearchText(searchString, scop§±±e: self.searchDisplayController.searchBar.scopeButtonTitles[self.searchDisplayController.searchBar.selectedScopeButtonIndex] as NSString)

    self.filterContentForSearchText(searchString, scope: self.searchDisplayController.searchBar.scopeButtonTitles[self.searchDisplayController.searchBar.selectedScopeButtonIndex ] as NSString) // HERE IS WHERE I GET THE ERROR

    return true
}

The cells array is populated but i get an error when I type a letter in the search Bar.

Apparently self.searchDisplayController.searchBar.scopeButtonTitlesis nil,How come?

Any idea why?

tudoricc
  • 709
  • 1
  • 12
  • 31

0 Answers0