使用搜索显示控制器时出现问题

时间:2014-09-04 11:59:03

标签: ios swift uisearchbar uisearchdisplaycontroller uisearchbardelegate

当我运行项目时尝试重新加载搜索字符串表时,我遇到了一个非常奇怪的问题

我收到错误消息:unexpectedly found nil while unwrapping an Optional value

代码是:

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
}

填充了单元格数组,但在搜索栏中输入字母时出现错误。

显然self.searchDisplayController.searchBar.scopeButtonTitles是零,为什么?

知道为什么吗?

0 个答案:

没有答案
相关问题