Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Vikram Pal
Courses Plus Student 307 PointsAdding Search Friends To Ribbit
This is what I have done so far using the guide in this link:
https://teamtreehouse.com/forum/how-to-implement-search-function-in-ribbit-app
Drag a search bar into the edit friends table view
I added the delegate
: UITableViewController <UISearchBarDelegate>
- Added the property
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
- Added this line
self.searchBar.delegate = self;
- Implemented these methods:
-
(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//dismiss keyboard [self.searchBar resignFirstResponder];
}
-
(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//Enable the cancel button when the user touches the search field self.searchBar.showsCancelButton = TRUE; }
-
(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
//dsiable the cancel button when the user ends editing self.searchBar.showsCancelButton = FALSE; }
-
(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
//dismiss keyboard [self.searchBar resignFirstResponder];
//Strip the whitespace off the end of the search text NSString *searchText = [self.searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//Check to make sure the field isnt empty and Query parse for username in the text field if (![searchText isEqualToString:@""]) {
PFQuery *query = [PFUser query]; [query whereKey:@"username" equalTo:searchText]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { //check to make sure the query actually found a user if (objects.count > 0) { //found a user //set the user as the table views data source and reload the table view //A user was not found, display error message } else { //no user found } [self.tableView reloadData]; } else { //error occurred } }];
} }
Now I don't understand what he was saying in the last part, where do I add the nil in which file. I can't find anything to change anywhere.
"when you find a user, you would need to set that user as the tableViews data source and reload the tableview. that will display the user that was found as the first cell in the table view.
As far as making the table view empty when the view appears just set the data source to nil in viewWillAppear. That will clear out the data anytime the view appears so you get a blank view when it appears, when you search and find a user the tableview shows that user, and when it appears again its blank again."
How do I finish the last part? ^^
Anything I did wrong?
1 Answer
Stone Preston
42,016 Pointsimplement viewWillAppear and in that method:
//lets say you are using a PFObject property called user as your tableViews data source
self.user = nil
of course since self.user is not an array, you will have to modify your numberOfRows method in your tableView to something like
if (self.user) {
//a user was found, we need to display one row.
return 1;
} else {
//a user was not found/searched for yet, dont display any rows
return 0
}
Vikram Pal
Courses Plus Student 307 PointsVikram Pal
Courses Plus Student 307 PointsGosh I feel so dumb. I can't get it to work. I have everything else but can't find where to implement he nil and how I should change the current code.