[英]UITableView: How to change cell height dynamically when a button is clicked in it?
I have a UITableView, in which each cell has a button. 我有一个UITableView,其中每个单元格都有一个按钮。
When the button is clicked, the cell's height will be changed. 单击该按钮时,单元格的高度将会更改。
Of course, the whole tableview height will be changed according to it. 当然,整个tableview高度将根据它改变。
I cannot find a way during long surfing. 在漫长的冲浪中我找不到方法。 What is an elegant solution to achieve this? 实现这一目标的优雅解决方案是什么?
Call the function - reloadRowsAtIndexPaths:withRowAnimation:
. 调用函数- reloadRowsAtIndexPaths:withRowAnimation:
. Implement function UITableViewDelegate
function - tableView:heightForRowAtIndexPath:
and return the new height. 实现函数UITableViewDelegate
函数- tableView:heightForRowAtIndexPath:
并返回新的高度。
(See this answer. Kudos to Tapas Pal for writing the original answer. I just changed it to better fit your question.) (请参阅此答案。感谢Tapas Pal撰写原始答案。我只是将其更改为更适合您的问题。)
You will need a BOOL
variable to tell the cell that its height should be different from the others. 您将需要一个BOOL
变量来告诉单元格其高度应与其他高度不同。 You will also need a number (here, I will use NSInteger
) variable so that you can increase the height for the right cell. 您还需要一个数字(这里,我将使用NSInteger
)变量,以便您可以增加右侧单元格的高度。
BOOL shouldCellBeExpanded = NO;
NSInteger indexOfExpandedCell = -1;
Then, in your -tableView:cellForRowAtIndexPath:
method, add the following to where you design your cell. 然后,在-tableView:cellForRowAtIndexPath:
方法中,将以下内容添加到您设计单元格的位置。 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. 此外,如果需要向单元格添加任何元素,可以在if
语句中执行。
[[cell aButton] setTag:[indexPath row]];
if(shouldCellBeExpanded && [indexPath row] == indexOfExpandedCell)
{
// design your read more label here
}
Moving to the button's IBAction
. 移动到按钮的IBAction
。 When the button is tapped, the UITableView
will reload the cell that button is on. 点击按钮时, UITableView
将重新加载按钮所在的单元格。 Note: if you use multiple sections in your UITableView
, you will need to add another number variable to account for that. 注意:如果您在UITableView
使用多个部分,则需要添加另一个数字变量来说明这一点。 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];
}
Finally, in the -tableView:heightForRowAtIndexPath:
method: 最后,在-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. 如果没有,原来的那个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.