xxtmanxx
xxtmanxx

Reputation: 209

Why is it not searching parse database?

I am trying to search parse db and have the results show up in a collectionview. All the hookups on storyboard are correct. When I click the search button, the keyboard does not disappear and the program does not attempt to search the database, or update the cell with the searched information. How do I fix this?

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var collectionview: UICollectionView!
var users = [PFObject]()

override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.delegate = self
}


 func loadUsers(){
    var query = PFQuery(className:"_User")

    if searchBar.text != ""{
        query.whereKey("username", containsString: searchBar.text)
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in

            // The find succeeded now process the found objects into the countries array
            if error == nil {

                // Clear existing country data
                self.users.removeAll(keepCapacity: true)

                // Add country objects to our array
                if let objects = objects as? [PFObject] {
                    self.users = Array(objects.generate())
                }

                // reload our data into the collection view
                self.collectionview.reloadData()
                print(self.users)
            } else {
                // Log details of the failure
                print("Error: \(error!) \(error!.userInfo)")
            }
        }

    }
}



func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return users.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionview.dequeueReusableCellWithReuseIdentifier("searchcell", forIndexPath: indexPath) as! searchcustomcell

    if let value = users[indexPath.item]["username"] as? String{
        cell.searchname.text = value
    }

    if let value = users[indexPath.item]["ProPic"] as? PFFile{
        let finalImage = users[indexPath.item][""] as? PFFile
        finalImage!.getDataInBackgroundWithBlock(){
            (imageData: NSData?, error: NSError?) -> Void in
            if error == nil{
                if let imageData = imageData{
                    cell.searchimage.image = UIImage(data: imageData)
                    }
            }
        }
    }
    print(users)


    return cell
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {

    // Dismiss the keyboard
    searchBar.resignFirstResponder()

    // Reload of table data
    self.loadUsers()

    print(users)
}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {

    // Clear any search criteria
    searchBar.text = ""

    // Dismiss the keyboard
    searchBar.resignFirstResponder()

    // Reload of table data
    self.loadUsers()
}

Upvotes: 0

Views: 34

Answers (1)

Alexander Li
Alexander Li

Reputation: 791

searchBarTextDidEndEditing is not being called. You need to use:

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
    searchBar.resignFirstResponder()
    self.loadUsers()
    print(users)
}

Upvotes: 1

Related Questions