Reuse of UITableViewCell, uitableviewcell
Reuse of UITableViewCellI. Cell Reuse Principle: when the list is rolled, some uitableviewcells will be removed from the window, And UITableView will put the UITableViewCell outside the window into an object pool, waiting for reuse. When UITableView requires dataSource to return UITableViewCell, dataSource will first view this object pool. If there is an unused UITableViewCell in the pool, dataSource will configure the UITableViewCell with new data and then return it to UITableView, re-display to the window to avoid creating new objects.
There is another very important issue: Sometimes you need to customize UITableViewCell (using a subclass to inherit UITableViewCell), and each row does not necessarily use the same UITableViewCell, therefore, a UITableView may have different types of UITableViewCell, and there are many different types of UITableViewCell in the object pool. Therefore, UITableView may get the UITableViewCell of the wrong type when reusing UITableViewCell.
Solution: UITableViewCell has an NSString * reuseIdentifier attribute. You can input a specific string identifier when initializing UITableViewCell to set the reuseIdentifier (generally the class name of UITableViewCell is used ). When UITableView requires dataSource to return UITableViewCell, it first uses a string to identify the corresponding type of UITableViewCell object in the object pool. If yes, It is reused. If no, this string identifier is passed in to initialize a UITableViewCell object.
Ii. Cell Performance Optimization
By default, UITableView only loads cells that appear on the screen. If a cell is not removed from the screen, it is stored in the cache pool.
Performance optimization steps:
Step 1: Define the cell ID (you do not need to create the cell id every time, so you need to use static and static identifiers. They will only be created for the first time and will not be created in the future .)
Step 2: retrieve cell from the cache pool
Step 3: Check whether the cell is empty. If the cell is empty, manually create the cell.
Iii. How to improve tableView Performance
A. Reuse cell
We all know that it takes time to apply for memory, especially frequent memory application within a period of time will cause a great deal of overhead, and the cell layout is the same in most cases in tebleView, at this time, we can improve the performance through the recycling mechanism.
B. Avoid re-layout of content
Try to avoid re-layout the cell when reusing the cell. Generally, the cell is well laid out when the cell is created.
C. Use an opaque subView
When customizing the cell, setting the subView to be added to an opaque state will greatly reduce the time required for rendering when multiple view layers are stacked.
D. If convenient, directly reload the drawRect method of subView.
If multiple small elements are required during cell customization, it is best to draw multiple projects to be displayed directly, instead of adding multiple subviews.
E. do not implement the delegate method of tableView unless necessary.
Many delegate functions in tableView further control cell attributes, such as the height of each cell, whether the cell can be edited, and the supported edit style, if not necessary, it is best not to implement these methods because the quick call of these methods will also affect the performance.