title: Adding search usernames feature to SnapChat like application
body: <p>I currently have made a SnapChat like application using a TreeHouse tutorial.</p>
  
  <p>The way the application was built, when you go to friends, then click edit on the top right, it shows you everyone that is registered. I don't want this, rather someone have to search for usernames. </p>
  
  <p>I've added a search bar to the edit friends view controller and connected it to the .m file. </p>
  
  <p>I was using help from this post but the search bar would not show any users when pressed: <a href="https://teamtreehouse.com/forum/search-friends-ribbit-almost-complete" rel="nofollow">https://teamtreehouse.com/forum/search-friends-ribbit-almost-complete</a></p>
  
  <p>This is my current code for the EditFriendsViewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import &lt;Parse/Parse.h&gt;
  
  @interface EditFriendsViewController : UITableViewController &lt;UISearchBarDelegate&gt;
  
  @property (nonatomic, strong) NSArray *allUsers;
  @property (nonatomic, strong) PFUser *currentUser;
  @property (nonatomic, strong) NSMutableArray *friends;
  @property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
  @property (nonatomic, strong) PFUser *user;
  
  - (BOOL)isFriend:(PFUser *)user;
  
  @end
  </code></pre>
  
  <p>This is my current code for the EditFriendsViewController.m </p>
  
  <pre><code>#import "EditFriendsViewController.h"
  #import "MSCellAccessory.h"
  
  
  @interface EditFriendsViewController ()
  
  @end
  
  @implementation EditFriendsViewController
  
  UIColor *disclosureColor;
  
  
  - (void)viewDidLoad
  {
  [super viewDidLoad];
  self.searchBar.delegate = self;
  PFQuery *query = [PFUser query];
  [query orderByAscending:@"username"];
  [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
      if (error) {
          NSLog(@"Error: %@ %@", error, [error userInfo]);
      }
      else {
         //set your user property to the first (and only) object in the objects array
  self.user = [objects objectAtIndex:0];
  [self.tableView reloadData];
  
  }
  }];
  
  self.currentUser = [PFUser currentUser];
  
  disclosureColor = [UIColor colorWithRed:0.553 green:0.439 blue:0.718 alpha:1.0];
  
  }
  
  - (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 &gt; 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
          }
      }];
  
  }
  
  
  }
  
  
  #pragma mark - Table view data source
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
  
  // Return the number of sections.
  return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
  
  // Return the number of rows in the section.
  return [self.allUsers count];
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier     forIndexPath:indexPath];
  
  PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
  cell.textLabel.text = self.user.username;
  
  if ([self isFriend:user]) {
      cell.accessoryView = [MSCellAccessory accessoryWithType:FLAT_CHECKMARK     color:disclosureColor];
  }
  else {
      cell.accessoryView = nil;
  }
  
  return cell;
  }
  
  #pragma mark - Table view delegate
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  
  {
  [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  
  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  
  PFUser *user = [self.allUsers objectAtIndex:indexPath.row];
  PFRelation *friendsRelation = [self.currentUser relationForKey:@"friendsRelation"];
  
  if ([self isFriend:user]) {
      cell.accessoryView = nil;
  
      for (PFUser *friend in self.friends) {
          if ([friend.objectId isEqualToString:user.objectId]) {
              [self.friends removeObject:friend];
              break;
          }
      }
  
      [friendsRelation removeObject:user];
  }
  else {
      cell.accessoryView = [MSCellAccessory accessoryWithType:FLAT_CHECKMARK
  color:disclosureColor];
      [self.friends addObject:user];
      [friendsRelation addObject:user];
  }
  
  [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      if (error) {
          NSLog(@"Error %@ %@", error, [error userInfo]);
      }
  }];
  }
  
  #pragma mark - Helper methods
  
  - (BOOL)isFriend:(PFUser *)user {
  for (PFUser *friend in self.friends) {
      if ([friend.objectId isEqualToString:user.objectId]) {
          return YES;
      }
  }
  
  return NO;
  }
  
  
  @end
  </code></pre>