title: Swift iOS : Find parse users via UISearchBar and dysplay results in a table view
body: <p>i trie to find parse users via UISearchBar and display results in a table view.
  When i heat search, nothing happened...
  My idea was to use the searchText.text as a query to PFUser.</p>
  
  <p>Should i try to use searchBar with search display controller instead ?
  Any idea ?</p>
  
  <pre><code>import UIKit
  
  
    class AllFriendsTableViewController: UITableViewController, UISearchBarDelegate,      UISearchDisplayDelegate {
  @IBOutlet var SearchDisplay: UISearchDisplayController!
  
  
  
  var userList:NSMutableArray = NSMutableArray()
  
  
   func searchBarShouldBeginEditing(searchBar: UISearchBar!) -&gt; Bool {
      return true
  }
  
  func searchBarShouldEndEditing(searchBar: UISearchBar!) -&gt; 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!) -&gt; 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!) -&gt; Int {
      return 1
  }
  
  override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -&gt; Int {
      if tableView == searchDisplayController.searchResultsTableView {
          return userList.count
      }
      else {
          return 0
      }
  }
  
  
  
  override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -&gt; 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!) -&gt; 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!)-&gt; 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.
  }
  </code></pre>
  
  <p>}</p>