UITableView: How to dynamically change cell height when a button is clicked?

I have a UITableView in which each cell has a button.
When the button is pressed, the cell height will be changed.
Of course, the entire height of the table will be changed in accordance with it.
I can not find a way during a long surf. What is an elegant solution for this?

+2
source share
2 answers

(see this answer. Claims to Tapas Pal for writing the original answer. I just modified it to better fit your question.)

BOOL, , . (, NSInteger), .

BOOL shouldCellBeExpanded = NO;
NSInteger indexOfExpandedCell = -1;

-tableView:cellForRowAtIndexPath: , . , , , . , - , if.

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

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

IBAction. , UITableView , ​​. . UITableView, . , .

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

, , , , , . , - . , .

+1

- reloadRowsAtIndexPaths:withRowAnimation:. UITableViewDelegate - tableView:heightForRowAtIndexPath: .

+1

Source: https://habr.com/ru/post/1629628/


All Articles