When I met this problem in the interview, I didn’t answer it all at that time, so I googled it. Some common things, such as Cell reuse, uniform Cell design, cache Cell height, and cache of Cell data resources, are actually used in daily life. However, due to lack of summary in daily life, You know how “== reuse ==” and “== cache ==” are all the answers you need to answer this question, but that’s not a good answer in an interview

In addition, the view control used in the Cell can drawRect as much as possible.

The result of the interview is also unknown, they first fall into a pit and gain wisdom, while it is hot to sum up.

Cell reuse mechanism

That’s the basic use of TableView, just a quick generalization.

[tableView dequeueReusableCellWithIdentifier:(NSString )identifier forIndexPath:(NSIndexPath )indexPath];

To use this method, you need to pre-register the Cell to the corresponding tableView:

  • Stroyboard: Define the Prototype of the Cell and set its Reusable Identifier



It is worth noting that Cell prototypes should be highly abstract and uniform, and as few as possible, because each different Prototype has its own Cell reuse pool, the more cells are created and the less efficient the reuse will be. (See the following Cell for unified specification design)

[registerNib:(nullable UINib )nib forCellReuseIdentifier:(NSString )identifier];

[registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier];

Design cells with uniform specifications

Unified Cell specifications can not only reduce the amount of code and NIB files needed to design different cells, but more importantly, improve Cell reuse rate and improve the overall performance of TableView.

  • A Cell of equal height is designed to display different data without much space.

  • Cells that dynamically calculate height should also be uniformly designed, such as the red rabbit example below




Although look a little different, but in fact the whole is unified, is the picture, name, company, position, time, content, other (pictures, web links, video and other accessories) as well as forward, praise, and comments at the bottom of the three buttons, these controls display according to different data in the model of dynamic control in the code.

Create a ViewModel that calculates and stores the Cell’s UI size information

@interface BYTweetViewModel : NSObject

@property(strong, nonatomic) BYTweetModel *dataModel; // Raw data model

@property(assign, nonatomic) CGFloat cellHeight; / / Cell height

  • (void)calculateCellHeight; // Calculate the height

@end

Here’s a pit to watch out for:

In iOS, the system is first called “tableView: heightForRowAtIndexPath:” get the height of each Cell will display, determine the layout of the entire UITableView. Then call “tableView: cellForRowAtIndexPath” access to the Cell. Therefore, the ViewModel is used to store UI information, the Cell is highly computed and the timing of use needs to be careful.

Process the data resources that the Cell needs to display in advance

Before the Cell is displayed, the raw data obtained from the server load is processed in the ViewModel in advance, including loading and compression of images, diversified display of rich text (NSString->NSAttributeString).

So the ViewModel might look something like this

@interface BYTweetViewModel : NSObject

@property(strong, nonatomic) BYTweetModel *dataModel; // Raw data model

@property(assign, nonatomic) CGFloat cellHeight; / / Cell height

// The data content to display

@property(strong, nonatomic) NSAttributeString *titleToShow;

@property(strong, nonatomic) NSAttributeString *contentToShow;

  • (void)calculateCellHeight; // Calculate the height

  • (void)handleSourceDataModel;

@end

other

I understand, is also commonly used schemes are basically the above several, in short, or I can go back to the interview when the height of the summary, as far as possible “reuse”, “cache”, with space for time.

In addition, there are some more extreme ways and operation details will not be further developed, roughly integrated.

  • Use as little transparency as possible with views in cells

  • Reduce the hierarchy of subviews

  • The image is loaded in the background process, and the loading process is canceled out of visual range

  • Use PNG for image resources whenever possible

reference

There is a discussion on Zhihu, which explains many different ideas. Although it is rarely necessary to be so extreme, you can also try to look at it when necessary. However, the corresponding amount of code increases, but you must pay attention to avoid various inexplicable bugs

Zhihu: How to enhance the smooth scrolling of iOS lists?