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

iOS

Ribbit: Edit Friends Issue. (The list update but not as the user want)

Hi there. I am making the Ribbit App. I can log in /out and sign up users. I have my list of friends as well. But I have an issue when I try to select my friends. Looks like there is certain in the execute command because when I try to change my friends is not really update as i want.

``òbjective-c // // EditFriendsTableViewController.m // Ribbit // // Created by mlabarga on 31/07/14. // Copyright (c) 2014 mayte. All rights reserved. //

import "EditFriendsTableViewController.h"

import <Parse/Parse.h>

@interface EditFriendsTableViewController ()

@end

@implementation EditFriendsTableViewController

  • (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]; }

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 =user.username;

    if ([self isFriend:user]) { cell.accessoryType =UITableViewCellAccessoryCheckmark; }

    else { cell.accessoryType =UITableViewCellAccessoryNone; }

    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.accessoryType = UITableViewCellAccessoryNone;

    for (PFUser *friend in self.friends) {
        if ([friend.objectId isEqualToString:user.objectId]) {
            [self.friends removeObject:friend];
            break;
        }
    }
    
    [friendsRelation removeObject:user];
    

    }

    else {

    cell.accessoryType =UITableViewCellAccessoryCheckmark;
    [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

1 Answer

The thing is that I can not only deselect one name. I need to deselect one and (at the same time) select other. Otherwise, I can not deselect the name that I want.