Can you animate a height change on a UITableViewCell when selected?

前端 未结 21 1024
天涯浪人
天涯浪人 2020-11-22 04:41

I\'m using a UITableView in my iPhone app, and I have a list of people that belong to a group. I would like it so that when the user clicks on a particular pers

相关标签:
21条回答
  • 2020-11-22 05:04

    I like the answer by Simon Lee. I didn't actually try that method but it looks like it would change the size of all the cells in the list. I was hoping for a change of just the cell that is tapped. I kinda did it like Simon but with just a little difference. This will change the look of a cell when it is selected. And it does animate. Just another way to do it.

    Create an int to hold a value for the current selected cell index:

    int currentSelection;
    

    Then:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        int row = [indexPath row];
        selectedNumber = row;
        [tableView beginUpdates];
        [tableView endUpdates];
    }
    

    Then:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if ([indexPath row] == currentSelection) {
            return  80;
        }
        else return 40;
    
    
    }
    

    I am sure you can make similar changes in tableView:cellForRowAtIndexPath: to change the type of cell or even load a xib file for the cell.

    Like this, the currentSelection will start at 0. You would need to make adjustments if you didn't want the first cell of the list (at index 0) to look selected by default.

    0 讨论(0)
  • 2020-11-22 05:08

    reloadData is no good because there's no animation...

    This is what I'm currently trying:

    NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
    

    It almost works right. Almost. I'm increasing the height of the cell, and sometimes there's a little "hiccup" in the table view as the cell is replaced, as if some scrolling position in the table view is being preserved, the new cell (which is the first cell in the table) ends up with its offset too high, and the scrollview bounces to reposition it.

    0 讨论(0)
  • 2020-11-22 05:09

    I found a REALLY SIMPLE solution to this as a side-effect to a UITableView I was working on.....

    Store the cell height in a variable that reports the original height normally via the tableView: heightForRowAtIndexPath:, then when you want to animate a height change, simply change the value of the variable and call this...

    [tableView beginUpdates];
    [tableView endUpdates];
    

    You will find it doesn't do a full reload but is enough for the UITableView to know it has to redraw the cells, grabbing the new height value for the cell.... and guess what? It ANIMATES the change for you. Sweet.

    I have a more detailed explanation and full code samples on my blog... Animate UITableView Cell Height Change

    0 讨论(0)
提交回复
热议问题