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

时间:2015-10-28 21:24:10

标签: ios uitableview tableviewcell

在我的.h文件中

    NSIndexPath *selectedCellIndexPath; 

在我的.m

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

使用此代码,我可以在点击时扩展单元格高度,但是我无法使其崩溃

1 个答案:

答案 0 :(得分:0)

当用户选择展开的单元格时,您必须将selectedCellIndexPath设置为nil:

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