Proper way to select/unselect buttons for cellForRowAtIndex

96 Views Asked by At

Firstly, my tableview is ordered much like a the contacts app so that "friends" are sorted by section alphabetically. Like, the Snapchat app, I want to select friends and have a button put a check mark next to their name upon clicking the button. I have solved this, but I am questioning whether or not this is the proper way to go about solving this problem. Here is how I am currently performing this.

-(void)checkBoxPressedOnCell:(SASendToTableViewCell *)cell
{
    NSIndexPath *selectedIndexPath = [self.tableView indexPathForCell:cell];

    //If there are already selected indexPaths, the user might be unchecking a box. Else, add the first checkbox as a selected indexPath.
    if (self.selectedIndexes.count) {
        BOOL deleted = NO;
        for (NSIndexPath *preSelectedIndexPath in self.selectedIndexes) {
            //if there the box is already selected remove it.
            if ([preSelectedIndexPath compare:selectedIndexPath] == NSOrderedSame) {
                deleted = YES;
                [self.selectedIndexes removeObject:preSelectedIndexPath];
                break;
            }
        }
        //If there was no indexPath to delete, add the selected indexPath
        if (!deleted) {
            [self.selectedIndexes addObject:selectedIndexPath];
        }
    }
    else {
        [self.selectedIndexes addObject:selectedIndexPath];
    }

    [self.tableView reloadRowsAtIndexPaths:@[selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Friends";
    SASendToTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.delegate = self;

    //Sort the sections, case insensitive, ascending
    NSArray *sortedSect = [self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:indexPath.section]];
    NSSortDescriptor * sortByName = [[NSSortDescriptor alloc] initWithKey:@"addDisplayName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    NSArray *sortedFriends = [sortedSect sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByName]];

    PFObject *friend = sortedFriends[indexPath.row];
    cell.displayName.text = friend[@"addDisplayName"];
    cell.toFriend = friend;


    //DISPLAYING THE CHECKMARK
    if (self.selectedIndexes.count) {
        for (NSIndexPath *selectedIndexPath in self.selectedIndexes) {
            if ([selectedIndexPath compare:indexPath] == NSOrderedSame) {
                cell.checkBoxButton.selected = YES;
                break;
            }
            else{
                cell.checkBoxButton.selected = NO;
            }
        }
    }
    else
        cell.checkBoxButton.selected = NO;

    return cell;
}
1

There are 1 best solutions below

3
On BEST ANSWER

The approach seems good to me. But you could save a lot of code by using NSArray's containsObject: method:

if ([self.selectedIndexes containsObject:selectedIndexPath]) {
    [self.selectedIndexes removeObject:selectedIndexPath];
}
else {
    [self.selectedIndexes addObject:selectedIndexPath];
}

and:

cell.checkBoxButton.selected = [self.selectedIndexes containsObject:indexPath];