ios8+ UITableView automatically calculates cell height and caches

Source: Internet
Author: User

In this article, let's talk about the cell adaptive height of uitableview and the solution to the problems encountered. Before reading the article, I hope you will have UITableView basic use.

The GitHub address for the demo of this article first: Uitableviewcellheightdemo. You can download it and read the article together.

History of cell height calculations

Before iOS8, if the height of UITableViewCell is dynamic, if you want to display the correct words, we need to return the exact height of each row in the proxy method below UITableView:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

If the cell has a lot of controls and the style is complex, we might need to write a lot of code to do some complicated calculations, and maybe even cause the slippage to be smooth.

Later, some people wrote some third parties to solve the problem, such as Uitableview-fdtemplatelayoutcell. As long as the cell is constrained from top to bottom, it can help us to calculate the cell's height and can be cached, eliminating the cost of writing our own code. Specifically, you can go to the link to see its demo.

But in the IOS10 system, the FDTemplateLayoutCell card interface, and the tableview of the number of rows of the more performance of the card.

And Apple after IOS8, introduced a super simple cell dynamic adaptive method, use FDTemplateLayoutCell is simpler, and now iOS10 are out, there is no need to support iOS7, so finally I chose to use the system approach. So we'll never have to write heightForRowAtIndexPath a method again hahaha.

How to use the cell adaptive height of the system

First, we need to put the controls on the cell top-down, and if you are unfamiliar with constraints, consider these two articles to learn:
Auto Layout Tutorial in IOS 9 part 1:getting Started (http://www.raywenderlich.com/115440/ AUTO-LAYOUT-TUTORIAL-IN-IOS-9-PART-1-GETTING-STARTED-2)
[Auto Layout Tutorial in IOS 9 part 2:constraints

It is possible to constrain with xib and masonry with code. Attention constraints must be added from top to bottom, let the system know how to calculate the height. In the demo of this article, the cell plus constraint is this:


Cell constraints

Add a good restraint and then tell TableView to adjust to the height. There are two ways to do this:

self.tableView.rowHeight = UITableViewAutomaticDimension;self.tableView.estimatedRowHeight = 100;

Or you can just write the proxy method.

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 100;}

The meaning of this is to tell TableView that you need to adapt to the height of your own, I do not count you ha ha. But we need to tell it a approximate height, such as the above 100, which in theory can be casually written, does not affect the display result, but the closer to the real height the better.

Look at the demo effect:


Demo

We see that the cell has its own adaptation of the content to calculate the height, is not very convenient to hem.
The specific code you can go to the demo to see OH.

In fact, section header and footer can also be automatically adapted, the corresponding methods are:

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;

But we in the actual development, generally there is no header and footer, and some words are usually given a fixed height. So it is not explained here, the principle is the same.

Possible problems and workarounds

1. Height misalignment
Sometimes it's possible to run out and see the cell's height appear wrong, just like this:


Height is wrong.


The problem is because the constraints do not meet top-down, so the system does not know how to calculate. The solution is to modify the constraint until it is satisfied. Be sure to understand the constraints well!

2. Click the status bar to scroll to the top
We know that if there is uiscrollview in the interface, clicking on the status bar will let it scroll to the top, like this:


Clicking on the status bar will scroll to the top

But if we use the method of automatic height calculation and call the Reloaddata method of TableView (for example, when our data has pagination, we will refresh the TableView when we have finished loading the next page of data). At this point, there will be problems, click on the status bar will have the chance to not accurately scroll to the top:


Untitled.gif

The solution to this problem is to cache the cell's height, the code is as follows:

@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//缓存高度所用字典
#pragma mark-uitableviewdelegate-(CGFloat) TableView: (UITableView *) TableView Estimatedheightforrowatindexpath: (Nsindexpath *) indexpath{NSNumber *height = [self.heightatindexpath Objectforkey:indexpath]; if (height) {return height.floatValue ; } else {return 100;} -(void) TableView: (uitableview *) TableView Willdisplaycell :(uitableviewcell *) cell Forrowatindexpath: (NSIndexPath * ) indexpath{nsnumber *height = @ (cell.frame.size.height); [self.heightatindexpath setobject:height ForKey:indexPath ];} 

To explain, a dictionary is used to make a container, and the cell's height will be saved in the dictionary when it is about to be displayed. Then, when calling the Estimatedheightforrowatindexpath method, go to the dictionary to see if there is a cache height, return it, and return a approximate height.

After the cache height, in the demo more than a few times, found that the click on the status bar has been able to accurately scroll back to the top of it:


Solved the problem with cache height

This code can actually be written in the base class of Viewcontroller, so that you can cache the height of the cell every place you write it over. See demo. That's perfect!



Wen/ios_ (Jane book author)
Original link: http://www.jianshu.com/p/64f0e1557562
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

ios8+ UITableView automatically calculates cell height and caches

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.