12

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
igrrik
  • 467
  • 1
  • 6
  • 16

2 Answers2

7

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

Community
  • 1
  • 1
igrrik
  • 467
  • 1
  • 6
  • 16
4

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.

Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • Thank you, for your answer. 1) As I said before this fix with reloading data doesn't work for me 2) I didn't understand how he exactly did it, and also he didn't use constraints, but I'll ask him – igrrik Feb 23 '15 at 16:12
  • @igrrik, maybe you can download the project from this [link](https://www.dropbox.com/s/rhza2amiuztpar8/SelfSizingDemoTemplate.zip?dl=0) and compare to yours and see what the differences are? This sample code is provided by this [blog](http://www.appcoda.com/self-sizing-cells/) and the `[self.tableView reloadData]` does work over there. Or maybe, if you would like to, you can post some related code and let us take a look at them?It maybe because of something else as well. – Yuchen Feb 23 '15 at 16:17
  • we've made some progress! First problem has been solved. But the second is still here. Well there is actually only two lines of code, that is provided by this blog-post: self.tableView.estimatedRowHeight = 239.0; self.tableView.rowHeight = UITableViewAutomaticDimension; Everything else is made with constraints. The difference between my project and their, is that I have an imageview in my cell and a detailview. – igrrik Feb 24 '15 at 18:46
  • @igrrik, how did you solve the first question? Did you solve it by reloading the table view? – Yuchen Feb 25 '15 at 14:09
  • @igrrik, seems that a lot of people are actually having issue with this IOS bug. Also have a look at this one: http://stackoverflow.com/a/25800080/1035008. At this point, I will suggest wait for Apple to fix their bug. If this jump is a critical issue for you, I would also recommend using some traditional way to make resizable cell as have been discussed in this post http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Yuchen Feb 25 '15 at 14:51
  • Well it turned out that the first problem still exists. Thank you very much for your help, I'll learn all of these topics and try to fix this problem. The topic provided in your answer looks very inspiring, I asked the author to give some more information. As soon as I have any results I tell you. – igrrik Feb 25 '15 at 15:03
  • @igrrik Okay, please keep me updated if you find a work around for this problem. – Yuchen Feb 25 '15 at 15:08
  • I've solved it. Look at my answer. Though the link, I've provided with similar question has another solution, I've found mine on apple developer forum. – igrrik Feb 26 '15 at 21:31