的UITableView:如何动态地更改单元格高度时,被点击一个按钮? [英] UITableView: How to change cell height dynamically when a button is clicked in it?

查看:152
本文介绍了的UITableView:如何动态地更改单元格高度时,被点击一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,其中每个单元有一个按钮。结果
当点击该按钮时,细胞的高度将被改变。结果
当然,整个的tableview高度将根据它来改变。结果,
我找不到长冲浪过程中的一种方式。
什么是优雅的解决方案来实现这一目标?

I have a UITableView, in which each cell has a button.
When the button is clicked, the cell's height will be changed.
Of course, the whole tableview height will be changed according to it.
I cannot find a way during long surfing. What is an elegant solution to achieve this?

推荐答案

(请参见这个答案。荣誉来小吃帕尔的写原来的答案,我只是改变了它以更好地适应你的问题。)

(See this answer. Kudos to Tapas Pal for writing the original answer. I just changed it to better fit your question.)

您将需要一个 BOOL 变量来告诉它的高度应该是和别人不同的单元格。您还需要一个数字(在这里,我将使用 NSInteger的)的变量,这样就可以增加高度右侧的单元格。

You will need a BOOL variable to tell the cell that its height should be different from the others. You will also need a number (here, I will use NSInteger) variable so that you can increase the height for the right cell.

BOOL shouldCellBeExpanded = NO;
NSInteger indexOfExpandedCell = -1;

然后,在你的 -tableView:的cellForRowAtIndexPath:方法,添加以下到您设计您的细胞。您将设置按钮的标签是与它的单元格的列,让你知道什么扩展单元。此外,如果你需要的任何元素添加到单元格,可以做到这一点如果语句内。

Then, in your -tableView:cellForRowAtIndexPath: method, add the following to where you design your cell. You will set the button tag to be the same as its cell's row so that you know what cell to expand. Moreover, if you need to add any elements to the cell, you can do it inside the if statement.

[[cell aButton] setTag:[indexPath row]];

if(shouldCellBeExpanded && [indexPath row] == indexOfExpandedCell)
{
   // design your read more label here
}

移动到按钮的 IBAction为。当按钮被点击时,的UITableView 会将该按钮上的单元格。的注:如果您在使用多个节的的UITableView ,你将需要添加另一个号码变量来解释这一点。如果您需要帮助评论。

Moving to the button's IBAction. When the button is tapped, the UITableView will reload the cell that button is on. Note: if you use multiple sections in your UITableView, you will need to add another number variable to account for that. Comment if you need help.

-(IBAction) aButtonTapped:(id)sender
{
    UIButton *aButton = (UIButton *)sender;
    indexOfExpandedCell = [aButton tag];
    shouldCellBeExpanded = YES;

    [[self tableView] beginUpdates];
    [[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: indexOfExpandedCell inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    [[self tableView] endUpdates];
}

最后,在 -tableView:heightForRowAtIndexPath:方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(shouldCellBeExpanded && [indexPath row] == indexOfExpandedCell)
        return 200.0f; //Your desired height for the expanded cell
    else
        return 100.0f; //The other cells' height
}

这样做是什么,并检查是否细胞有望扩大如果是这样,该小区目前是否被要求高度值之一。如果是,则返回值是展开高度。如果不是,则原来的

What this does is check if a cell is expected to be expanded and, if so, whether that cell is the one currently being asked for a height value. If it is, then the returned value is the expanded height. If not, the original one.

这篇关于的UITableView:如何动态地更改单元格高度时,被点击一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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