Optimization of cell in UITableView, uitableviewcell

Source: Internet
Author: User

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;


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.