当我重用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
}
}
答案 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)
}