当重用UISearchController时,我得到致命的错误:数组索引超出范围

时间:2015-07-03 19:22:42

标签: ios swift parse-platform uisearchcontroller

当我重用UISearchController时,我收到致命错误:数组索引超出范围。我读了一些材料,但我找不到答案。请帮帮我。

我的代码在

下面
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1 ?? 0 // 1 ?? 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var query: PFQuery = PFQuery(className: "NewsNow")

    if self.dataSearchResults == nil {
        return self.objects?.count ?? 0 
    } else {
        return self.dataSearchResults?.count ?? 0
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我修复了func updateSearchResultsForSearchController

func updateSearchResultsForSearchController(searchController: UISearchController) {
    self.dataSearchResults?.removeAll(keepCapacity: false)
    var searchText = searchController.searchBar.text
    var query: PFQuery = PFQuery(className: "NewsNow")
    if searchController.active == true  {
        query.whereKey("nameNews", matchesRegex: searchText, modifiers: "i")
        self.tableView.reloadData() 
    }
    query.findObjectsInBackgroundWithBlock { (results:[AnyObject]?, error: NSError?) -> Void in
        self.dataSearchResults = results as? [PFObject]
        self.tableView.reloadData()
    }
     println(searchText)
}