如何检查所选单元格是否被重新选择

How to check if selected cell got reselected

我有一个自定义 UITableViewCell,它扩展了选择范围。我想要做的是让单元格高度恢复正常(44),如果被选择的单元格被重新选择。这是我的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath isEqual:self.selectedCell] && ![[tableView indexPathForSelectedRow] isEqual:indexPath]) {
        return 100;
    }
    return 44;
}

代码运行良好,但似乎忽略了 if 语句中的第二项。显然我做错了。有办法解决吗?

我不知道你为什么要拿 cell 和 indexPath 比较。

[indexPath isEqual:self.selectedCell]

如果您只想展开选定的单元格。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[tableView indexPathForSelectedRow] isEqual:indexPath]) 
    {
        return 100;
    }
    return 44;
}

UITableViewDelegate 有取消选择方法

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

你需要在这个委托中重新加载传感器

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

你可以这样做:

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSMutableArray* selectedIndexPaths;

@end
<...>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:CELL_ID];
    cell.textLabel.text = [NSString stringWithFormat:@"%i", (int)indexPath.row];

    return cell;
}


#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CGFloat height = 44.0;
   if ([_selectedIndexPaths containsObject:indexPath])
   {
       height = 100;
    }

   return height;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//store or remove selected indexPaths
    if ([_selectedIndexPaths containsObject:indexPath])
    {
        [_selectedIndexPaths removeObject:indexPath];
    }
   else
   {
        [_selectedIndexPaths addObject:indexPath];
    }

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

我想你想要的是在点击选定的单元格时取消选择单元格。

存储selectedIndex路径并在delegate中进行比较

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

    if ([selectedIndexPath isEqual:indexPath])
    {
         [tableView deselectRowAtIndexPath:indexPath animated:1];
    }
    else
    {
        selectedIndexPath = indexPath;
    }          
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

您需要将该特定索引路径存储在数组中以表明它已展开假设您选择了 5 行将行号存储在数组中,并在选择它后重新加载该行

这是可以帮助您实现此目的的小代码片段

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

if([_arrExpandedRows containsObject:indexPath.row]){
     [_arrExpandedRows removeObject:indexPath.row];
    }else{
     // [_arrExpandedRows removeAllObjects]; // IF you only want to make it work for single row
      [_arrExpandedRows addObject:indexPath.row];
    }
[tableView reloadData];
}

在你的heightForRowAtIndexPath

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([_arrExpandedRows containsObject:indexPath.row]){ 
    {
        return 100;
    }
    return 44;
} 

那么,如果您的数组包含扩展行的对象,它会做什么,它将降低其行高并返回到 44,否则它将返回到 100。

以上代码也适用于多行同时展开和折叠。