Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 233
igrrik Avatar asked Feb 23 '15 13:02

igrrik


People also ask

Is UITableVIew scrollable?

Smooth Scrolling:- As a result, it affects the smoother scrolling experience. In UITableVIew, Scrolling will be smooth through cell reuse with the help of prepareForReuse() method.

What is UITableVIew?

UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.

How do you find the height of a cell?

On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Row Height. Tip: To quickly autofit all rows on the worksheet, click the Select All button, and then double-click the boundary below one of the row headings.


1 Answers

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

like image 106
igrrik Avatar answered Oct 14 '22 16:10

igrrik