单选 UITableView 叠加单元格

Single selection UITableView overlay cell

我正在尝试定制 select 个 UITableView 细胞。为此,我在我的单元格上方创建了 UIView,现在触摸时 appear/disappear。但问题是当我按下单元格时 selection 出现。如果那时我 select 任何其他行,它也会被 selected!但它不能。之前的每个单元格都必须 deselected 但我只为我的 UITableView 做单个 selection。我很乐意提供任何帮助。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
 {
 static NSString *CellIdentifier = @"userDataCell";
 AVMThemeCell *cell = [self.userContentTable dequeueReusableCellWithIdentifier:CellIdentifier]; 

 // Configure the cell...
if (cell == nil) {
 cell = [[AVMThemeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
  AVMDataStore *oneItem = [userContentArray objectAtIndex:indexPath.row];
  oneItem = [userContentArray objectAtIndex:indexPath.row];

 [cell setGenre:oneItem];
 cell.imgView.image = [UIImage imageNamed:imgThemesArray[indexPath.row]];



 //save cell state!

 NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];
 if ([selectedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
 {
     NSLog(@"selectedCellsArray %@",selectedCellsArray);
     cell.selectedBG.hidden=NO;
     [cell setSelected:NO animated:YES];
 }
 else
 {

     cell.selectedBG.hidden=YES;

 }


 return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
@try{
    // TODO: Select Item
    if (shareEnabled) {

        [selectedCellsArray removeAllObjects];
        AVMThemeCell *collectionCell = (AVMThemeCell*)[tableView cellForRowAtIndexPath:indexPath];
        NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:(unsigned int)indexPath.row];

        if ( ![selectedCellsArray containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]  )
        {
            [selectedCellsArray addObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];

            collectionCell.selectedBG.hidden = NO;
            [collectionCell setSelected:NO animated:YES];
          //  NSLog(@"view is %@",collectionCell.selectedBG);
            NSLog(@"selected view is hidden = %hhd",collectionCell.hidden);
            NSLog(@"selected in didselect %d",(int)indexPath.row);
        }
        else {
            [selectedCellsArray removeObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
            collectionCell.selectedBG.hidden = YES;
           NSLog(@"DEselected in didDEselect");
        }

    }
} @catch (NSException *e){
    NSLog(@"Exception! %@",e);
}



}

一个更简单的方法是使用 NSIndexPath.

创建 NSIndexPath 变量来跟踪最后选择的单元格。

@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

viewDidLoad()方法中的初始化变量:

self.selectedIndexPath = [NSIndexPath indexPathForRow:-1 inSection:-1];

观察上一行中行的值 -1 和部分的值 -1,这将在 tableView 中没有行选择的情况下初始化 indexPath。

现在,UITableView 数据源方法将如下所示:

cellForRowAtIndexPath 方法中放置一个条件来检查当前的 indexPath 是否被选中?

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

    // Change background color of selected cell

    if (self.selectedIndexPath == indexPath) {
        [cell setBackgroundColor:[UIColor orangeColor]];
    } else {
        [cell setBackgroundColor:[UIColor clearColor]];
    }

    // . . .
}

didSelectRowAtIndexPath 方法中更新选定的索引路径:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // . . .

    AVMThemeCell *previousCell = (AVMThemeCell *)[tableView cellForRowAtIndexPath:self.selectedIndexPath];
    previousCell.backgroundColor = [UIColor clearColor];

    self.selectedIndexPath = indexPath;
    AVMThemeCell *selectedCell = AVMThemeCell *[tableView cellForRowAtIndexPath:self.selectedIndexPath];
    selectedCell.backgroundColor = [UIColor orangeColor];

    // . . .
}