如何动态扩展tableview中的单元格? [英] how do I expand cell in tableview dynamically?

查看:40
本文介绍了如何动态扩展tableview中的单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只有一个 cell ,并且在 tableview 中有一个按钮. cell 的高度在

I have only one cell with a button in a tableview. The cell height is defined in

tableView:tableView:heightForRowAtIndexPath:

像元高度为40;

单击按钮时,如何将像元高度更改为80?谢谢您的帮助.

When I click the button, how do I change the cell height to 80? thanks for any help.

推荐答案

定义一个属性并 @synthesize 对其进行设置:

Define a property and @synthesize it:

@property (retain, nonatomic) NSIndexPath* selectedRowIndex;

覆盖 didSelectRowAtIndexPath heightForRowAtIndexPath 方法:

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

    if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {

        //[_yourTableView deselectRowAtIndexPath:indexPath animated:YES]; If you only want the cell to get bigger
        selectedRowIndex = nil;

    }

    else {  self.selectedRowIndex = [indexPath retain];   }

    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

if (tableView == _yourTableView) {


    if(selectedRowIndex && indexPath.row == selectedRowIndex.row){

        return 80;
    }

    // Cell isn't selected so return single height
    return 40;

}

else return 0;
}

由于beginUpdates和endUpdates行这应该使单元格高度动画化.

Because of the beginUpdates and endUpdates lines this should animate the cell height.

这篇关于如何动态扩展tableview中的单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆