The optimization of cell in UITableView

Source: Internet
Author: User

Reuse of 1.cell

The so-called cell reuse is that when the view is loaded, only the cell in the current view is created, or a bit more than the current view, when the view scrolls, the cell that rolls out the screen is put into the cache, and the cell that rolls into the screen gets the cell from the cache according to identifier. This repeats itself so that only fixed cell objects are created, saving memory.

Here's the code for reusing cells:

1. Defining identity for reuse

static NSString *reuseid = @ "TG";

2, retrieving the reuse cell from the cache

Cztgcell *cell = [TableView Dequeuereusablecellwithidentifier:reuseid];

3. Determine if cell is equal to nil to create cell

if (cell = = nil) {

cell = [[[NSBundle mainbundle]loadnibnamed:@ "Cztgcell" Owner:nil Options:nil]lastobject];

}

return cell;

2. Why define a reuse identifier when it is defined as a static

The number of cell calls is quite large, creating multiple identifier objects, while static variables create only one chunk of memory, saving memory space.

The point of note for the 3.cell init method

Overriding the Init method, when adding a child control, in this method, you need to set the control's one-time property, the so-called one-time property, refers to the size of the text, such as the color of the text ah, such as fixed values, because these properties and data do not have a relationship, improve performance.

Note: To set the control's frame, the frame headview directly in the Init method has no effect, so to set the frame of each child control in the Layoutsubviews method, the method must override the parent class's method.

4.cell reuse, not only to overwrite the data, but also to overwrite the corresponding state, or reuse will be the state and data will be reused

Reuse Workaround: Override the model's set method to overwrite the data and state.

The problem of 5.cell row height

The row height is computed several times before the cell is created, and if the row height is fixed, use the following proxy method sparingly, as it will call and calculate the row height multiple times, using the TableView RowHeight property

-(CGFloat) TableView: (UITableView *) TableView Estimatedheightforrowatindexpath: (Nsindexpath *) Indexpath;

The optimization of cell in UITableView

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.