简体   繁体   English

在一个部分中仅允许检查一个UITableViewCell

[英]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. 我试图在一个部分中一次只允许检查一个UITableViewCell。 What I'm going for is to uncheck a checked UITableViewCell (if there is one) and checking the current selected cell. 我要去的是取消选中已检查的UITableViewCell(如果有的话)并检查当前选定的单元格。 So basically, If one CellA is selected, and I select CellB, I want CellA to unselect and CellB to select. 因此,基本上,如果选择了一个CellA,而我选择了CellB,则我希望CellA取消选择,而CellB选择。

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. 我将通过为第1部分和第2部分创建一个属性(类型为NSIndexPath)和为第0部分创建一个数组属性来跟踪各个部分中所选的一个或多个单元格来做到这一点。 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). 轻按一个单元格时,可以使用indexPath设置属性的值,或者在数组的情况下添加indexPath(如果已经检查,则将其删除)。 Then check the state of those properties or array in cellForRowAtIndexPath. 然后在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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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