Ios 选择单元格时切换单元格高度

Ios 选择单元格时切换单元格高度,ios,uitableview,tableviewcell,Ios,Uitableview,Tableviewcell,在我的.h文件中 NSIndexPath *selectedCellIndexPath; 在我的电脑里 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { selectedCellIndexPath = indexPath; [tableView reloadRowsAtIndexPaths:[NSArray arrayWit

在我的.h文件中

    NSIndexPath *selectedCellIndexPath; 
在我的电脑里

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    selectedCellIndexPath = indexPath;  
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
}  

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(selectedCellIndexPath != nil && [selectedCellIndexPath compare:indexPath] == NSOrderedSame){
        return 80;  
    } else if ([selectedCellIndexPath compare:indexPath] != NSOrderedSame){
    return 40;
    }

    return 40;  
}  

有了这段代码,我可以在点击时扩展单元格高度,但是我无法使其折叠

当用户选择扩展单元格时,您必须将
selectedCellIndexPath
设置回零:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedCellIndexPath = (self.selectedCellIndexPath && [self.selectedCellIndexPath compare:indexPath] == NSOrderedSame) ? nil : indexPath;
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

当你想折叠单元格时?@vienvu当你点击一个展开的单元格时,如果你想编码的话,会有一种切换,但是你介意为我解释一下这个片段吗?它的语法我以前没见过,特别是问号。您的名字是什么?nil:indexath;这是一个三元运算符。语法是
let x=a?b:c
a
是一个布尔值。如果
a==true
那么
x=b
,如果
a==false
那么
x=c
换句话说,这与写
如果a{x=b}或者{x=c}是一样的,没问题,很高兴它帮助了你!