문제

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.

도움이 되었습니까?

해결책

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
        // ...
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top