[IOS] understanding of tableviewcell and the significance of reuseidentifier

Source: Internet
Author: User

I believe many people are familiar with the following code.

When using tableview, the following code is required, the most standard, and the core:

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * cmaincell = @ "cmaincell"; uitableviewcell * cell = [tableview partition: cmaincell]; if (cell = nil) {Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cmaincell] autorelease];} // content cell of the custom cell. textlabel. TEXT = @ "XXX"; return cell ;}

You can understand this code in this way.

Uitableviewcellll has a queue used to store the cells generated but hidden by scrolling tableview,

For example, a table has 20 cells,

However, the screen can only display 5 (of course, iPhone 5 may display 6), so there will be other cells not displayed,

But it will appear when sliding tableview.


In the Code:

Static nsstring * cmaincell = @ "cmaincell ";
Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cmaincell];

It is to extract a cell not used for the moment from the queue according to the identifier, only when the cell is nil, that is, when there is no old cell in the queue,

To execute:

Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cmaincell] autorelease];

To generate a new cell.

If there is an old one, you do not need to execute this statement. This saves resources and is considered as a type of reuse.


No cell exists in the queue during tableview initialization. Therefore, this code is executed once every cell is generated:

Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cmaincell] autorelease];

When the screen is full and the next row is scroll up, the first row is hidden and placed in the queue,

Then a new line of execution statement is added:

Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cmaincell];

The result is not nil. Then, Statement 2 is skipped, which saves resources.

Of course, there is no problem with the cell format provided by the system (left-right structure,

However, if you add some controls to the cell (you can customize the tableview of the cell ),

For example, when adding a label or button, problems may occur sometimes. Note that sometimes.


For example, the label text of each cell is different.


First, the process of adding must be in the statement:

If (cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cmaincell] autorelease];
}

Then, this is the one-time addition,

If it is placed in the statement:

Cell. textlabel. Text = @ "XXX ";

Then, because cell Reuse may already have a label on the old one, you can add another one, resulting in multiple additions.


Second, the text value of the label must be reset every time, that is, in the statement:

Cell. textlabel. Text = @ "XXX ";

In this way, each execution must be performed.


If it is placed in the statement:

When the old cell is used, the old label text will still be retained. This is wrong.

So it is in:

If (cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cmaincell] autorelease];
}

After adding, in:

Cell. textlabel. Text = @ "XXX ";


You can set a tag value when adding the tag, so that you can get the control settings through the tag value later.

Statements:

Static nsstring * cmaincell = @ "cmaincell ";

If the structure of each cell is identical, This identifier is used,


However, if the cell structures are different, some have textfield, some have buttons, and some have switches,


So we can't reuse each other. We can only use different identifiers for each line,


It can be easily used (@ "cmaincell % d", indexpath. Row) to ensure that there are no duplicates,

Add group numbers to multiple groups.


Is it necessary to reuse cells?

Uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cmaincell];
If (cell = nil ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cmaincell] autorelease];
}

Yes.


Because it can be reused when you scroll and hide it again.

Identifiers can use a new one at a time, but in order to maximize reusability and save resources, they have come up with so many ways.



The clever use of tags can make your cell impeccable.

Related Article

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.