Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Redraw TableViewCells with Dynamic Cell heights on rotation

How can I go about getting my cells to redraw once the device is rotated?

When I am testing on iPhone, the default orientation is portrait, so my dynamic cells are as tall as they need to be when the device is in portrait orientation: this means when I rotate to landscape, there is a lot of empty space in the cell. When I test on iPad, it's the opposite. I get part of my text cut off when rotating to portrait orientation, as the default is landscape. I have separate storyboards for both devices, and both view controllers are set to "Orientation: Inferred"

enter image description here (Portrait) enter image description here (Landscape)

This is what's happening on iPhone - the stretching label has a dark red background so you can see the actual size of the label. However, the cell height is not recalculating and since the bottom labels are constrained to the bottom of the cell, they are all the way at the bottom by themselves. If I add a constraint from the stretching label (the one with the background) to the bottom of the cell, all it does is stretch the label like so:

enter image description here (Label is stretched)

What this tells me is that right now I'm just unable to get the cell to know what the orientation is and to redraw once it changes to fit. Here is my heightForRowAtIndexPath code that is calculating the height of the cell:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.opCell)
        {
            self.opCell = [self.container dequeueReusableCellWithIdentifier:@"OPCell"];
        }
    self.opCell.opName.text = [[self.data objectAtIndex:indexPath.row] valueForKey:@"name"];
    [self.opCell layoutIfNeeded];
    CGFloat height = [self.opCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    return height + 1;
}

So far to get it to rotate I have tried [self.container reloadData], where container is my TableView with willRotateToInterfaceOrientation and didRotateToInterfaceOrientation to no avail. I also tried putting it in viewDidLoad, so as to maybe detect and load when the view is loaded in landscape mode with the correct height, but this didn't work either. For iPhone, no matter what I do it loads the height for portrait view and does not update on rotation.

I have found a few questions that I thought might help, but I can't seem to get this answer working for me, even though it seems like the same question I have, and this one seems to focus more on cell width than height and I'm not sure that it applies in the same way.

Here is even a tutorial that, when I ran it on the iPhone, is also not resizing the TableViewCells on rotation.

This tutorial seemed to have some insight into changing table heights, so I added the begin/endUpdates code into my didRotateToInterfaceOrientation, thinking that might cause a change, but this didn't work as well.

So, if anyone has any suggestions or knows a solution, it would be much appreciated, as I'm hitting a brick wall with this one!

like image 664
Eichhörnchen Avatar asked Jul 07 '14 22:07

Eichhörnchen


1 Answers

It seems you are doing a word wrap here. So as per my prior experience I suggest you to :
1) use sizeWithFont or the newer iOS7 version with appropriate linebreakmode.
2) You can implement layoutSubview for your "opCell" where you can redefine the frame size.
3) Calculate the cell height during the rotation.. ensure that you properly set the width as per the new tabelView Bounds.
4) Also you can implement estimatedHeightForRowAtIndexPath for quick estimated size for table reload operation.

Hope this helps !!

like image 109
Vik Avatar answered Nov 11 '22 04:11

Vik