12
votes

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.

2
did u use constraints for the label and the image view in your cell?Teja Nandamuri
Yes, there are 3 constraint for image: 1)leading space 2)top space 3) trailing space; 3 constraint for first label: 1)leading space 2)top space 3) trailing space; 4 constraints for second label 1)leading space 2)top space 3) trailing space 4) bottom spaceigrrik
what are the bounds of the label1 when you apply the constraints?Teja Nandamuri
The width is 290, height is 21igrrik

2 Answers

7
votes

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

4
votes

Unfortunately I believe both of the questions that you ask about are IOS bugs.

Question (1)

A easy fix is suggested by this blog.

When the table view is first displayed, you may find some of the cells are not sized properly. But when you scroll the table view, the new cells are displayed with correct row height. To workaround this issue, you can force a reload after the view appears:

Simply add the following to your viewDidAppear method. I have tested it and it works very well.

[self.tableView reloadData];

Question (2)

This second question is a duplicate of this following question:

IOS 8 UITableView self-sizing cells jump/shift visually when a new view controller is pushed

A workaround is suggested by the author of the question himself, which seems okay. However, I have not tried out this one yet.

Okay, I solved it by caching my cell heights in sizeThatFits, and returning that value for estimated cell heights within the delegate. Works beautifully.

Feel free to head over to that question for other proposed solutions.