⬆️ ⬇️

Acceleration of UITableView on iPhone

In one application I am developing, I used UITableView to display information. And here I will tell you what methods were invented to speed up the work of this element.



image



The list of genres is UITableView.

UITableView consists of UITableViewCell cells. You can report the number of cells in the list, the height of each cell, divide them into sections. When the cell needs to be shown, the function is called:

- (UITableViewCell *) cellForRowAtIndexPath: (NSIndexPath *) indexPath

The mechanism of operation is such that if the cell is hidden behind the screen, then at the next show the application will again request the cell.



The implication is that you must create a cell there, fill it with content and return it. But constantly creating new cells is wasteful, and is only suitable for those cells that contain few elements.

')

In my application, there were about 10 different elements per cell (UILabel, UITextView, UIImageView). At the same time, about 5 such cells were displayed on the screen. There were wild brakes when scrolling. And after long experiments, I had a whole list of possible scrolling accelerations of such heavy lists. Presented in order of chronology and their speed.



1) - (UITableViewCell *) dequeueReusableCellWithIdentifier: (NSString *) identifier

This function allows you to save time on creating a cell from NIB files, it returns the cell that has already been created, but you will still have to change its contents to the current one, and this time.

This is the method that Apple itself advises. He is often found in their examples.

Cons :

- it takes time to replace the content cell

Pros :

- memory savings



2) Pre-create all cells and store them in NSArray or NSMutableArray.

In this case, the cell is created once, and stored in an array. Every time a cell is requested, we just need to get it from the array by index. Alternatively, you can not create all the cells at once, but only when requested, and then save to an array. In both cases, the cell will be created only once.

This method had to be used because of the presence of animation in the cells.

Cons :

- memory is required for all cells

- it is possible hang up while all cells are created at once

Pros :

- recoil rate, time is spent only on access to the element of the array



3) UIScrollView

This option means instead of UITableView to use UIScrollView, that is, UIView which can be scrolled. We create all the cells, place them as a subView in our UIScrollView, set the scrolling dimensions. Ready.

This method was used by torment. Method 2 did not suit for scrolling speed.

Cons :

- memory is required for all cells

- quickly worked only to a height of 1500 pixels

- complexity in the implementation of interaction with the user

Pros :

-faster scrolling



And when our team was completely discouraged, they remembered another option that was used.



4) Screenshots of cells.



The bottom line is what to do with the screenshots of the cells. Brakes were apparently caused by a large number of subViews (cell elements). Well then let's do instead of 10 subView, 1 which contains a graphic representation of all ten.



MyCell is an original cell already filled with the necessary content.

UIGraphicsBeginImageContext(((UIView*)MyCell.frame.size);

[((UIView*)MyCell.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();



The image saved in image can be added to a cell with a single UIImageView.

It turned out well, very fast list with cells.

Cons :

- it takes time to create

- requires at least 2 times more memory for storing cells and their screenshots

Pros :

- the fastest scrolling



UPD:

People say that if you use the first method, everything is fine drawn, unfortunately I can not use this method as I use:

- 3 types of different cells

- there is an animation on the cells



Now counted the number of elements in the cells, counted 22 per cell.

Source: https://habr.com/ru/post/88879/



All Articles