Search code examples
iosobjective-cuitableviewreloaddata

Self Sizing Cells make UITableView jump


I've got a UITableView, each cell has an image and two labels, as you can see on the first picture

enter image description here

So I am trying ti use self-sizing cells and everything is great, except 2 things:

1) First and Second cell don't show content properly, but after I scroll down and then return to them everything is ok. I tried to use [tableview reloadData] in viewDidAppear, but it doesn't help. Watch the labels at first they don't fit. You can watch it on this video https://www.youtube.com/watch?v=I9DGBl1c5vg

Look at the labels on the first cell.

2) It's a tough one. I'm scrolling the table and everything is great, but the problem happens, when I select the row. It's pushing me to detail view, but when I press "back" and return to master view, the tableview jumps,so I'm coming back not to the selected row, also if I scroll the table it doesn't scroll smooth, but jumps. Here are the links for two videos, first one shows that scroll is smooth https://www.youtube.com/watch?v=9wAICcZwfO4 if i don't go to detail view, and the second shows the jumping problem https://www.youtube.com/watch?v=fRcQ3Za1wDM .

It's absolutely sure connected with self sizing cells, because if I don't use it, none of this problem happens.


Solution

  • Okay, I solved both of the problems.

    Question 1

    I've added these two lines

    [cell.contentView setNeedsLayout];
    [cell.contentView layoutIfNeeded];
    

    in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath before return cell

    Question 2

    The solution appeared to be very simple. I've just needed to implement viewWillDissapear and reload data in it, so the code looks like this

    - (void)viewWillDisappear:(BOOL)animated;{
        [super viewWillDisappear:animated];
        [self.tableView reloadData];
    }
    

    Solution for me was simple, if it doesn't work for someone, here I found some useful links.
    1) UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift)
    2) http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html