Optimization of cell in UITableView, uitableviewcell
1. cell Reuse
The so-called cell reuse means that when a view is loaded, only the cell in the current view will be created, or the cell will be a little more than the current view. When the view is rolling, the cell that gets out of the screen will be put into the cache, and the cell that rolls into the screen will get the cell from the cache according to the Identifier. This round robin will only create a fixed cell object, saving the memory.
The code for reusing cells is as follows:
// 1. Define the reuse identifier
Static NSString * reuseId = @ "tg ";
// 2. Obtain the reuse cell from the cache
CZTgCell * cell = [tableView dequeueReusableCellWithIdentifier: reuseId];
// 3. Determine whether to create a cell if the cell is equal to nil.
If (cell = nil ){
Cell = [[[NSBundle mainBundle] loadNibNamed: @ "CZTgCell" owner: nil options: nil] lastObject];
}
Return cell;
2. Why do I define static identifiers
Cell is called many times, and multiple identifiers are created. Static variables are created only once to open up a piece of memory, saving the memory space.
3. cell init method considerations
Override the init method. When adding a child control, you need to set the one-time attribute of the control in this method. The so-called one-time attribute, it refers to fixed values such as text size, text color, and so on, because these attributes have no relationship with data and improve performance.
Note: Setting the frame of the control has no effect on the headView frame directly in the init method. Therefore, to set the frame of each sub-control in the layoutSubviews method, you must override the parent class method.
4. When the cell is reused, it should not only overwrite data, but also overwrite the corresponding state. Otherwise, both the State and data will be reused during reuse.
Reuse solution: overwrite the data and status in the set Method of the model.
5. cell Row Height Problem
Cell calculates the Row Height multiple times before it is created. If the row height is fixed, use the following proxy method as little as possible because it will call and calculate the Row Height multiple times, you can use the rowHeight attribute of tableView.
-(CGFloat) tableView :( UITableView *) tableView estimatedHeightForRowAtIndexPath :( NSIndexPath *) indexPath;