UITableView点击展开cell

1.定义控制cell的两个变量
    //最近打开的index
    int currentClickIndex;
   
    //是否打开cell
    BOOL isOpenCell;

2.给变量赋值
- (void)viewDidLoad
{
    [super viewDidLoad];
    currentClickIndex = -1;
    [self.answerTableView registerClass:[AnswerTableViewCell class] forCellReuseIdentifier:cellString];
   
}

3.控制cell返回的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (indexPath.row == currentClickIndex) {
        if (isOpenCell == YES) {
            currentClickIndex = indexPath.row;
            return [self.dataHeights[indexPath.row] floatValue] + 44.0f;
        }
         return 44.0f;
    }else{
        return 44.0f;
    }
}

4.点击时改变操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
    if (indexPath.row == currentClickIndex) {
        isOpenCell = !isOpenCell;
    }else{
        isOpenCell = YES;
    }
   
    currentClickIndex = indexPath.row;
   
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

ok,这样就可以做一个点击展开cell了。


猜你喜欢

转载自wfkbyni.iteye.com/blog/2054580