0

Here is my method in my 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]);
        }
    }];
}

When I run the app, everything works just fine with no errors or warnings but when I tap on friends to remove the checkmark by their name it does not happen.

Dave Chen
  • 10,887
  • 8
  • 39
  • 67
Jared Gross
  • 649
  • 3
  • 9
  • 23
  • 1
    add a breakpoint at the line where you set the accessoryType to none to check that `if ([self isFriend:user])` can become true. If the break point is not triggered when you tap on friends, fix `isFriend:`. – Matthias Bauch Aug 24 '13 at 23:45
  • http://stackoverflow.com/questions/1750753/uitableviewcell-accessory-type-checked-on-tap-set-other-unchecked – Fabio Cardoso Aug 24 '13 at 23:47

1 Answers1

0

This code syntax might help you check that it's working. Then you'll probably see as Matthias said that the issue is coming from your isFriend: method because your code looks fine for me.

Or another way, I think it's cleaner to separate the two logics of selecting/deselecting, so don't forget to enable the "multiple selection" on the tableview :

self.tableView.allowsMultipleSelection = YES;

And on the tableview delegate :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

Then to retrieve all selected items :

- (void)handleSelectedItems
{
    NSArray *selectedItemIndexPaths = [self.tableView indexPathsForSelectedRows];
    for (NSIndexPath *indexPath in selectedItemIndexPaths) {
        PFUser *user = [self.allUsers objectAtIndex:indexPath.row];

        // do what you want
        // ...
    }
}
Community
  • 1
  • 1
dulaccc
  • 1,148
  • 11
  • 12
  • Here's what I am ultimately trying to accomplish. I would like to set up a tableView that allows users to select/deselect friends and save the data for which friends are selected when the user taps 'next'. What I have right now is a tableView that selects the cell and puts a checkmark to the right of the cell.. but when I click on the cell again to remove the checkmark, nothing happens. – Jared Gross Aug 25 '13 at 02:46
  • I've been struggling with this one issue for almost 6 hours now, please help!! – Jared Gross Aug 25 '13 at 03:44
  • Have you tried the code included in my answer ? Try it without any of your business logic just like I edited it, it's only performing the select/deselect operation, then we'll nail down the saving problem. – dulaccc Aug 25 '13 at 10:41
  • Yep! I got it working with your suggestions. The cells are now checked and can be unchecked by tapping them again! hooray! Now for the saving of selected friends... thanks so much for the help! – Jared Gross Aug 25 '13 at 22:32
  • Glad that it helps :) now for the saving part, just write a method that will be called when the next button is tapped and use `[self.tableview indexPathsForSelectedRows]` to get an array of all the selected index paths. – dulaccc Aug 26 '13 at 14:29
  • So very helpful dulaccc! is there a way to create the index path array from cells that have checkmarks assigned rather than selected rows? – Jared Gross Aug 27 '13 at 09:44
  • No there is no method for an array of cells with checkmarks, instead I really recommend using `indexPathsForSelectedRows` like I said, so I've again updated my code to use this – dulaccc Aug 27 '13 at 12:28
  • Does it work for you ? if it does, please consider marking the answer as the solution ;) – dulaccc Aug 30 '13 at 12:35