选择/取消选择cellForRowAtIndex按钮的正确方法

时间:2015-06-08 18:01:10

标签: ios objective-c uitableview

首先,我的tableview的排序方式与联系人应用程序非常相似,因此“朋友”按字母顺序按部分排序。比如,Snapchat应用程序,我想选择朋友,并在点击按钮时在按钮旁边放置一个复选标记按钮。我已经解决了这个问题,但我在质疑这是否是解决这个问题的正确方法。以下是我目前的表现方式。

-(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 个答案:

答案 0 :(得分:0)

这种方法对我来说似乎很好。但是,您可以使用NSArray containsObject:方法保存大量代码:

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

cell.checkBoxButton.selected = [self.selectedIndexes containsObject:indexPath];
相关问题