简体   繁体   中英

Allow only one UITableViewCell to be Checked in a section

I'm trying to allow only one UITableViewCell to be checked at a time in a section. What I'm going for is to uncheck a checked UITableViewCell (if there is one) and checking the current selected cell. So basically, If one CellA is selected, and I select CellB, I want CellA to unselect and CellB to select.

Here's what I've done to try and accomplish this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(![self.selectedIndexPaths containsObject:indexPath]) {
        [self.selectedIndexPaths addObject:indexPath];
    }
    else {
        [self.selectedIndexPaths removeObject:indexPath];
    }

    if (indexPath.section == 0) {
        [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO; 
    }
    else if (indexPath.section == 1) {
        if (![self.selectedIndexPaths containsObject:indexPath]) {
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
        }
        else {
            for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
                if (indexPath2.section == 1) {
                    [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES;
                    [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
                }
            }
        }
    }
    else if (indexPath.section == 2) {
        if (![self.selectedIndexPaths containsObject:indexPath]) {
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
        }
        else {
            for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
                if (indexPath2.section == 2) {
                    [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES;
                    [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
                }
            }
        }

    }

}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if ([tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
        }
        else {
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
        }
    }
    else if (indexPath.section == 1) {
        if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
        }
        else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
        }
        else {
            for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
                if (indexPath2.section == 1 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) {
                    [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
                }
            }
        }
    }
    else if (indexPath.section == 2) {
        if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
        }
        else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){
            [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
        }
        else {
            for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
                if (indexPath2.section == 2 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) {
                    [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
                }
            }
        }

    }
}

As you can see, I have three sections, and the first one the user can select however many cells they want, but in the second and third, it's limited to one.

The Issue: Whenever I select a cell and then another cell, it works. If I select the first cell, both of them are now checked.

I would appreciate any help or suggestions. Thanks!

Your code looks a lot more complicated than it needs to be. I would do it by creating a property (of type NSIndexPath) for sections 1 and 2, and an array property for section 0 to keep track of the selected cell or cells in the respective sections. Set the value of the properties with the indexPath, or add the indexPath in the case of the array, when a cell is tapped (or delete it if it's already checked). Then check the state of those properties or array in cellForRowAtIndexPath.

@interface TableController ()
@property (strong,nonatomic) NSArray *theData;
@property (strong,nonatomic) NSIndexPath *selectedPathForSection1;
@property (strong,nonatomic) NSIndexPath *selectedPathForSection2;
@property (strong,nonatomic) NSMutableArray *selectedPaths;
@end

@implementation TableController 


- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectedPaths = [NSMutableArray new];
    self.selectedPathForSection1 = nil;
    self.selectedPathForSection2 = nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.theData[indexPath.section][indexPath.row];
    if ([self.selectedPathForSection1 isEqual:indexPath] || [self.selectedPathForSection2 isEqual:indexPath] || [self.selectedPaths containsObject:indexPath]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            if (! [self.selectedPaths containsObject:indexPath]) {
                [self.selectedPaths addObject:indexPath];
            }else{
                [self.selectedPaths removeObject:indexPath];
            }
            break;
        case 1:
            if (![self.selectedPathForSection1 isEqual:indexPath]) {
                self.selectedPathForSection1 = indexPath;
            }else{
                self.selectedPathForSection1 = nil;
            }
            break;
        case 2:
            if (![self.selectedPathForSection2 isEqual:indexPath]) {
                self.selectedPathForSection2 = indexPath;
            }else{
                self.selectedPathForSection2 = nil;
            }
            break;
    }
    [tableView reloadData];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM