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 Build a Self-Destructing Message iPhone App Relating Users in Parse.com Adding Checkmarks When the Table View is Loaded

Gareth Holton
Gareth Holton
258 Points

My CheckMarks aren't showing up

I can't seem to make the checkmarks appear they are there when i click on them but aren't saved when the page is refreshed any ideas? if you want specific pieces of code i can provide it thanks in advance !

6 Answers

Francisco Navarro
Francisco Navarro
14,185 Points

I think it would be useful if you post the didSelectRowAtIndexPath: method

Aaron Malone
Aaron Malone
15,762 Points

Do you mean that the checkmarks aren't showing on the main friends list (not the 'edit friends' list)? That's normal; making the checkmarks appear there will actually be covered later in the course when you learn about the ViewController lifecycle.

I think it's a problem with this video that this isn't actually addressed here -- it confused me too at the time.

Aaron Malone
Aaron Malone
15,762 Points

though if you do mean the checks aren't showing up when you go back to 'edit friends', then that is a bug and we would need to see code. :)

Gareth Holton
Gareth Holton
258 Points

apologies for the slight difference in code as i stupidly went onto the next video and didn't test! schoolboy error i know hope this helps ! thanks again

''' objective c

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

}

-(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.currenUser relationForKey:@"friendsRelation"];

if([self isFriend:user]) {

// 1st swtp wil remove the check mark

    cell.accessoryType = UITableViewCellAccessoryNone;


    //the 2nd will remover fro the array of friends

    for(PFUser *friend in self.friends) {

        if([friend.objectId isEqualToString:user.objectId]) {
            [self.friends removeObject:friend];
            break;

        }}
            // the 3rd will then remover from the back end of parse
       [friendsRelation removeObject:user];
}

else {

       cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.friends addObject:user];
   [friendsRelation addObject:user];
}

[self.currenUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (error) {
        NSLog(@"error %@ %@", error, [error userInfo]);
    }}];

}

-(BOOL)isFriend:(PFUser *)user {

for(PFUser *friend in self.friends) {

    if([friend.objectId isEqualToString:user.objectId]) {

        return YES;
    }}

    return NO;

} '''

Aaron Malone
Aaron Malone
15,762 Points

If you add friends in the 'edit friends' screen, then stop the app in Xcode and run it again, do the friends show up then? If so, this is related to the ViewController lifecycle issue I mentioned above and will be resolved later.

Gareth Holton
Gareth Holton
258 Points

I've done what you said and nope the 'edit friends' screen appears with no checkmarks once the app hasd been reload also when i go back to the 'friends' screen then back the 'edit friends' screen again the checkmarks have been removed again the link with the friends page to parse works as the friends appear and I've also removed relation on the parse website and the app is then refreshed again I'm also wondering if its to do with the prepare for segue method not sending the new friends across to the 'edit friends' screen

''' objective-c

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender: (id)sender {

    if ([segue.identifier isEqualToString:@"showEditFriends" ]) {

    EditFriendsViewController *viewController = (EditFriendsViewController *)segue.destinationViewController;
    
    viewController.friends = [NSMutableArray arrayWithArray:self.friends];
    

    } '''

Aaron Malone
Aaron Malone
15,762 Points

Not sure, then. The code looks fine from what I can see. You've got a typo of currenUser instead of currentUser in the edit controller, but as long as it's consistent that shouldn't break it (and if it wasn't consistent, I don't think your code would compile).

Gareth Holton
Gareth Holton
258 Points

Yeah didn't see that typo thanks and no your right it was the same through out and I've changed it to 'currentUser' now and still have the same issue ill delve into it some more and ill post an answer for future reference !

Francisco Navarro
Francisco Navarro
14,185 Points

It seems that friendsRelation is not working fine. Have you already check if...

PFRelation *friendsRelation = [self.currenUser relationForKey:@"friendsRelation"];

...is returning what is expected? If you add a user it appears in parse.com?

Gareth Holton
Gareth Holton
258 Points

yes its creating a 'friendRelation' column in parse and i can see the relations of the users i can also new fiends can be added from the 'edit friends screen' and then can be viewed in the 'friends' screen so I think there has been a relation created between the users on the back end thanks for the help!

Gareth Holton
Gareth Holton
258 Points

Just and Update for you both I've found that it wasn't running my prepare for segue code

'''-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if([segue.identifier isEqualToString:@"showEditFriends"]) {
    EditFriendsViewController *viewController = (EditFriendsViewController *)segue.destinationViewController;
    viewController.friends = [NSMutableArray arrayWithArray:self.friends];
}

} '''

because in the link between the view controllers in the storyboard i hadn't put my 'identifier' in now i have done this and copied the equal to string text to avoid typos it throws up and error and is crashing on the prepare for segue with and error of SIGBART

'''

int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } } '''

in the log its terminating on uncaught expectation of type NSExpectation

hope this helps !