r/iOSProgramming • • Nov 28 '15

Question Issue with expanding UITableViewCell

I'm trying to implement expanding and collapsing UITableViewCells in my application, I read up on *- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath )indexPath and [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; and [tableView beginUpdates]; [tableView endUpdates]; and I seem to be able to grasp how this should work. Unfortunately, I have a strange issue with how the expanding cell is animated, it seems that the UIView that comes into view when the cell is resized (in blue) is also displayed below the cells that are lower than the expanded cell in the UITableView, here's a screenshot, the final position. Is there any way to hide the part of the expanded cell before the cells below reach the correct position?

Here's the full project if anyone wants to give this a try: http://s000.tinyupload.com/index.php?file_id=56495324939814650050

4 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/JCD2020 Nov 29 '15

Indeed that's the case, those setFrame updates are completely unneeded, I just tested it. Do you know what might be causing those separators to glitch? Something to do with the height of the cell being insufficient to show it fully after expansion? Or something to do with cell.selectionStyle = UITableViewCellSelectionStyleNone; ?

1

u/patterware Nov 30 '15

If you change your storyboard, making the tableView use multiple section then you can remove the isSelected property from your Car class. Then the following changes to your ViewController class can be used, and will eliminate the separator glitch:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL selected = [tableView.indexPathsForSelectedRows containsObject:indexPath];
    if (selected)
    {
        return 44 + 150;
    }
    else return 44;
}

  • (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ [tableView beginUpdates]; [tableView endUpdates]; } -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView beginUpdates]; [tableView endUpdates]; }

If your car list might grow longer than a screen's worth, you would be wise to set a prototype table cell in your storyboard (or register a class or nib with the table view). This will allow the table view to properly cache and reuse cells. It will also eliminate the need for the cell creation code in your ViewController since [dequeueReusable...] will no longer return nil (in your current code it will always return nil).