Note that there are some explanatory texts on larger screens.

plurals
  1. POMake A UISearch Bar in iOS
    primarykey
    data
    text
    <p>I have this code:</p> <pre><code>for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) { [tableView deselectRowAtIndexPath:indexPath animated:NO]; } </code></pre> <p>I have a search bar that filters out search objects and puts a check mark on the cell when you click it. However, if you search and click on the first cell, it checks it off. But if you delete the search text, the check mark shows up on the first cell, not the one that I checked off. Here is a video showing this: <a href="https://docs.google.com/file/d/0B1Of4oDADQCQZkF0aGdhdmF0VjA/edit" rel="nofollow">https://docs.google.com/file/d/0B1Of4oDADQCQZkF0aGdhdmF0VjA/edit</a> Where do I put the code above in the code below so that the checkmark stays with the selected cell?</p> <pre><code>#import "MSAddFriendsViewController.h" @interface MSAddFriendsViewController () @end @implementation MSAddFriendsViewController - (void)viewDidLoad { [super viewDidLoad]; PFQuery *query = [PFUser query]; [query orderByAscending:@"username"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (error) { NSLog(@"Error %@, %@", error, [error userInfo]); } else { self.allUsers = objects; [self.tableView reloadData]; } }]; self.currentUser = [PFUser currentUser]; self.searchUsers.delegate = self; } - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if (searchText.length == 0) { self.userResults = nil; [self.tableView reloadData]; return ; } self.userResults = [NSMutableArray array]; for (PFUser *user in self.allUsers) { if ([user.username rangeOfString:searchText options:(NSAnchoredSearch | NSCaseInsensitiveSearch)].location == 0) { [self.userResults addObject:user]; } } [self.tableView reloadData]; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { if (self.userResults != nil) { return [self.userResults count]; } // 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; if (self.userResults != nil) { user = [self.userResults objectAtIndex:indexPath.row]; } else { user = [self.allUsers objectAtIndex:indexPath.row]; } cell.textLabel.text = user.username; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; PFRelation *friendsRelation = [self.currentUser relationforKey:@"friendsRelation"]; PFUser *user = [self.allUsers objectAtIndex:indexPath.row]; [friendsRelation addObject:user]; [self.currentUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (error) { NSLog(@"Error %@ %@", error, [error userInfo]); } }]; } @end </code></pre> <p>Can someone please correct the code above?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload