0

我试图在一个部分中一次只检查一个 UITableViewCell 。我要做的是取消选中已选中的 UITableViewCell (如果有的话)并检查当前选定的单元格。所以基本上,如果选择了一个 CellA,我选择了 CellB,我希望 CellA 取消选择,CellB 选择。

这是我为尝试实现这一目标所做的工作:

- (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;
                }
            }
        }

    }
}

如您所见,我有三个部分,第一个部分用户可以选择他们想要的任意多个单元格,但在第二个和第三个部分中,它仅限于一个。

问题:每当我选择一个单元格,然后选择另一个单元格时,它就可以工作。如果我选择第一个单元格,现在它们都被选中。

我将不胜感激任何帮助或建议。谢谢!

4

1 回答 1

1

您的代码看起来比它需要的复杂得多。我将通过为第 1 节和第 2 节创建一个属性(类型为 NSIndexPath)以及为第 0 节创建一个数组属性来跟踪相应部分中的选定单元格或单元格来做到这一点。使用 indexPath 设置属性的值,或者在数组的情况下添加 indexPath,当点击一个单元格时(或者如果它已经选中,则将其删除)。然后在 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];
}
于 2013-09-05T01:05:53.760 回答