Understanding of iOS development-uitableview reuse mechanism

Source: Internet
Author: User

Introduction

For a uitableview, you may need to display hundreds or thousands of cells, which can consume a lot of memory if each cell is created separately. In order to avoid this situation, the reuse mechanism was born.

Assuming that a UITABLEVIEW has 100 data to display, which requires 100 cells, while the screen can only display 10 cells at a time, there is one way to create a 100cell without creating a 11 (10+1).

Understand
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    staticNSString *identifier = @"cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (!cell) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];    }    return cell;}

Let's understand this code:

staticNSString *identifier = @"cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

The purpose of these two codes is to take a cell from the reuse queue based on the identifier identifier (what is the reuse queue first), since the first reuse queue is empty, so the cell is empty, if (!cell) condition is set, it will execute the code inside {}

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

Create a cell of type uitableviewcellstyledefault and identify it as identifier (@ "cell") so that a cell is created. When the 2nd cell is needed, it is also first taken from the reuse queue, found or not, then continue to create. As you can imagine, you need to create 10 cells when you first enter this page, and their identifiers are identifier.

When we pull down tableview, we know that there will be a 11th cell. This is the time to pick from the reuse queue, found that no, continue to create a 11th cell and return, this time, when the 11th cell is fully displayed, the 1th cell is exactly gone, where is it? The 1th cell (labeled Cell1) is put in the reuse queue .

If you continue to pull, the 12th cell will appear. So, do you want to continue creating it? Previously said to create a total of 11 cells, then all the cells have been created, then the 12th cell is how to come? Again, call the Dequeuereusablecellwithidentifier: method, look for the cell from the reuse queue, this time the queue has cell1, it will be taken out, when the IF (!cell) condition is not established, Will not create a new cell, the cell is returned as a 12th cell. As you can imagine, when the 12th cell is fully displayed, the 2nd cell disappears completely into the reuse queue, and then the cell (2) will appear as a 13th cell. It's so magical!

This is the reuse mechanism, although it requires 100 cells, but in fact only 11 cells are created, which are reused to play different roles when needed (just a change of clothes or the same person).

Identifier

You can see that when you create a cell with a identifier binding, this identifier can be understood as the cell identifier that identifies which reuse queue it belongs to.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

Take a look at this code, remove a cell from the reuse queue, notice the incoming parameter identifier, and if the reuse queue is likened to a room, then identifier is like the room number, marked to be looking for someone from a designated room (that is, cell). In addition, the queue will be based on the identifier of the cell into the specified re-queues.

As you can imagine, because all the cells in the above code use the same identifier, they will only go in and out of a reuse queue. If you change the code to this:

-(uitableviewcell  *) TableView: (uitableview  *) TableView Cellforrowatindexpath: ( nsindexpath  *) indexpath{nsstring  * identifier = [nsstring  stringwithformat:@ "cell%d"    , Indexpath.row ]; uitableviewcell  *cell = [TableView dequeuereusablecellwithidentifier:identifier    ]; if  (!cell) {cell = [[uitableviewcell  alloc]initwithstyle:uitableviewcellstyledefault reuse    Identifier:identifier]; } return  Cell;}  

When you create a cell, you bind a different identifier to each cell, and each cell is placed in a different queue when it is queued so 第一遍 The drop-down of 100 cells is after each call to Dequeuereusablecellwithidentifier cannot find the cell in the corresponding reuse queue, so to create 100 cells, this increases the consumption.

Register cell
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];

You can use this method to register the cell when creating TableView, the role of registration is that each time you take the cell from the reuse list, if it does not exist, the system will automatically help us create the cell, and bind the identifier identifier. As you can imagine, after registering you do not need to write this code:

if (!cell) {    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];}
Resolve list Repetition Confusion problem
- (UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) indexpath{Static NSString*identifier = @"Cell";UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:identifier];if(!cell) {cell = [[UITableViewCellAlloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifier]; }UILabel*label = [[UILabelAlloc]initwithframe:cgrectmake ( -,0, -, -)]; Label. Text= [NSStringstringwithformat:@"Test%d%d%d",(int) Indexpath. Row,(int) Indexpath. Row,(int) Indexpath. Row]; [Cell Addsubview:label];returnCell;}

We added a sub-view to each cell label, after running repeatedly pull up pull, you will find a list of chaotic phenomenon!

If you understand the nature of reuse, it's not hard to know why. Simply put, because every time a new cell is used, adding a label to it is another label on the basis of the original label, which shows confusion. Here's how to fix it:

Method One

The method

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

Replace with the following method

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

This method discards the reuse mechanism, each time retrieving the cell return from the Indexpath.

Method Two

Setting a different identifier for each cell, as described earlier, also solves the problem of list reuse, although it retains the reuse mechanism, but still needs to create 100 cells, the cost is not high.

Method Three

Delete the child view of the reuse cell, that is, each time the cell is removed from the reuse list, all its child views are removed, so there is no confusion, which means that the method retains the reuse mechanism, and the number of cells created is minimized and the performance is relatively high. The code is as follows:

- (UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) indexpath{Static NSString*identifier = @"Cell";UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:identifier];if(!cell) {cell = [[UITableViewCellAlloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:identifier]; }//Remove all child views[Cell. Subviewsenumerateobjectsusingblock:^ (IDobj, Nsuinteger idx,BOOL*stop) {UIView*subview = (UIView*) obj;    [SubView Removefromsuperview]; }];//Add new View    UILabel*label = [[UILabelAlloc]initwithframe:cgrectmake ( -,0, -, -)]; Label. Text= [NSStringstringwithformat:@"Test%d%d%d",(int) Indexpath. Row,(int) Indexpath. Row,(int) Indexpath. Row]; [Cell Addsubview:label];returnCell;}

At this point, the reuse mechanism of UITableView is complete.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding of iOS development-uitableview reuse mechanism

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.