Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Robert Conti
23,274 PointsiOS: deselectRowAtIndexPath method not working
In the Self-Destructing MSG App project in the iOS track, the 'deselectRowAtIndexPath' method is called when adding/removing friends. Also, a checkmark is either shown or hidden next to each username to indicate that they have been added or removed. For some reason, when tapping on a username in the 'Edit Friends' View Controller, the username stays highlighted and the checkmark does not appear/disappear until another username is tapped. So essentially, 'deselectRowAtIndexPath' is not working. Suggestions?
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(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; //1. Remove Checkmark
for (PFUser *friend in self.friends) { //2. Remove from the array of friends
if ([friend.objectId isEqualToString:user.objectId]) {
[self.friends removeObject:friend];
break;
}
}
[friendsRelation removeObject:user]; //3. Remove from the backend
}
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]);
}
}];
}
1 Answer

Stone Preston
42,016 Pointsyou implemented the wrong method. you implemented didDeselectRowAtIndexPath, you need to implement didSelectRowAtIndexPath. replace what you have with
//implement didSelectRow, not didDeselectRow
- (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; //1. Remove Checkmark
for (PFUser *friend in self.friends) { //2. Remove from the array of friends
if ([friend.objectId isEqualToString:user.objectId]) {
[self.friends removeObject:friend];
break;
}
}
[friendsRelation removeObject:user]; //3. Remove from the backend
}
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]);
}
}];
}
before you had implemented didDeselectRow, which is not the right one
//this is the wrong method
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
Robert Conti
23,274 PointsRobert Conti
23,274 PointsGood catch. Appreciate your help!