gpt4 book ai didi

ios - swift iOS : Find parse users via UISearchBar and dysplay results in a table view

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:03 25 4
gpt4 key购买 nike

我尝试通过 UISearchBar 查找解析用户并在 TableView 中显示结果。当我热搜时,什么都没发生...我的想法是使用 searchText.text 作为对 PFUser 的查询。

我应该尝试将 searchBar 与搜索显示 Controller 一起使用吗?有什么想法吗?

import UIKit


class AllFriendsTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
@IBOutlet var SearchDisplay: UISearchDisplayController!



var userList:NSMutableArray = NSMutableArray()


func searchBarShouldBeginEditing(searchBar: UISearchBar!) -> Bool {
return true
}

func searchBarShouldEndEditing(searchBar: UISearchBar!) -> Bool {
return true

}

func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
loadUser()
}



@IBOutlet var searchText: UISearchBar!



func loadUser () {

userList.removeAllObjects()
var findUser:PFQuery = PFUser.query()
findUser.whereKey("username", equalTo: searchText.text)


findUser.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
if !(error != nil) {
// The find succeeded.
println("succesfull load Users")
// Do something with the found objects
for object in objects {
self.userList.addObject(object)
println("users added to userlist")
}
self.tableView.reloadData()
} else {
// Log details of the failure
println("error loadind user ")
println("error")
}

}
}


override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
if tableView == searchDisplayController.searchResultsTableView {
return userList.count
}
else {
return 0
}
}



override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {



let cell: AllFirendsTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)as AllFirendsTableViewCell


let users:PFObject = self.userList.objectAtIndex(indexPath!.row) as PFObject


var findUserName:PFQuery = PFUser.query()
findUserName.whereKey("username", containsString: searchText.text)



findUserName.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!) -> Void in

if !(error != nil) {

if let user:PFUser = users as? PFUser {
cell.userNameTextField.text = user.username
println("user exist")
// define avatar poster

if let avatarImage:PFFile = user["profileImage"] as? PFFile {
avatarImage.getDataInBackgroundWithBlock{(imageData:NSData!, error:NSError!)-> Void in

if !(error != nil) {

let image:UIImage = UIImage(data: imageData)


cell.avatarImage.image = image as UIImage
cell.avatarImage.layer.cornerRadius = 24
cell.avatarImage.clipsToBounds = true

}

}

}
else {
cell.avatarImage.image = UIImage(named: "Avatar-1")
cell.avatarImage.layer.cornerRadius = 24
cell.avatarImage.clipsToBounds = true
}
}

}


}

return cell

}



override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
navigationController.navigationBar.hidden = false
loadUser()

}


override func viewDidAppear(animated: Bool) {

}

override func viewDidLoad() {


// Do any additional setup after loading the view, typically from a nib.
}

最佳答案

我怀疑这可能是因为您试图从发生网络请求的后台线程重新加载表格 View 。 UI 更新应该从主线程发生。

在您的 loadUser() 函数中尝试从主线程重新加载您的 tableview,如下所示:

NSOperationQueue.mainQueue().addOperationWithBlock({
self.tableView.reloadData()
})

这可能会解决您的问题。

关于ios - swift iOS : Find parse users via UISearchBar and dysplay results in a table view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25645243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com