Expanding UITableViewCell

2020-02-16 08:47发布

问题:

I am trying to expand a UITableViewCell to reveal more content. So far i've got the animation down, but my problem is the new content appears before the animation is complete. Here is my code:

ps the description label and read button both start off as hidden when the cell is configured.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedRow = indexPath;
    SDJCell *cell = (SDJCell *)[tableView cellForRowAtIndexPath:indexPath];
    [tableView beginUpdates];
    [tableView endUpdates];
    cell.descriptionLabel.hidden = NO;
    cell.readButton.hidden = NO;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    SDJCell *cell = (SDJCell *)[tableView cellForRowAtIndexPath:indexPath];
    [tableView beginUpdates];
    [tableView endUpdates];
    cell.descriptionLabel.hidden = YES;
    cell.readButton.hidden = YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(selectedRow && indexPath.row == selectedRow.row) {
    return 100;
    }
    return 44;
}

Any ideas for how I can reveal this extra content after the cell is done expanding??

Thanks!!

回答1:

I had the exact same problem. The solution is to set the autoresizing mask of the cell, so that it resizes along with its superview.

cell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
cell.clipsToBounds = YES;

you could also do this in sotoryboard, both flags can be set under identity inspector