2

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

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

该代码工作正常,但似乎忽略了 if 语句中的第二项。显然我做错了。有没有办法解决它?

4

4 回答 4

3

我不知道您为什么将单元格与 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];
于 2015-02-06T04:21:37.667 回答
1

您需要将该特定索引路径存储在数组中以指示它已展开假设您选择了 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。

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

于 2015-02-06T05:13:06.700 回答
0

你可以这样做:

@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];
}
于 2015-02-06T05:03:34.053 回答
0

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

存储 selectedIndexPath 并在委托中进行比较

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

    if ([selectedIndexPath isEqual:indexPath])
    {
         [tableView deselectRowAtIndexPath:indexPath animated:1];
    }
    else
    {
        selectedIndexPath = indexPath;
    }          
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
于 2015-02-06T05:05:08.180 回答