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 trialRobert 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!