你好我一直在看視頻來做這個代碼,但沒有任何工作我試圖圍繞代碼玩,仍然沒有得到什麼錯。問題是我沒有任何錯誤,當我運行代碼時,它不會在tableView中顯示結果。我想要做的是我想在解析用戶列表中搜索。 下面的代碼:快速使用搜索欄解析
func searchBarSearchButtonClicked(searchBar: UISearchBar){
searchBar.resignFirstResponder()
print("search word =\(searchBar.text)")
let usernameQuery = PFQuery(className: "User")
usernameQuery.whereKey("username", containsString: searchBar.text)
usernameQuery.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error != nil {
let myAlert = UIAlertController(title:"Alert", message:error?.localizedDescription, preferredStyle:UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
if let objects = results! as? [PFObject]{
self.searchResults.removeAll(keepCapacity: false)
for object in objects{
let usernameid = object.objectForKey("username") as! String
self.searchResults.append(usernameid)
}
dispatch_async(dispatch_get_main_queue()){
self.myTable.reloadData()
self.mySearchBar.resignFirstResponder()
}
}
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)
myCell.textLabel?.text = searchResults[indexPath.row]
return myCell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
mySearchBar.resignFirstResponder()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar){
mySearchBar.resignFirstResponder()
mySearchBar.text = ""
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}
}
什麼不完全正常工作?你嘗試調試它嗎? – Ammo
當我使用搜索欄時,它不會在tableveiew中顯示結果 –