About Cell reuse of uitableview

Source: Internet
Author: User

Uitableview is a control with high usage during iOS development. For me, almost every view I make has its own figure. But for a long time, I had a superficial understanding of her. Two things that touched me a lot. One is the custom uitableviewcell mentioned above, and the other is the reuse that we will mention today.

The so-called reuse of the surface meaning to understand is reuse. The general working principle is: uitableview is lazy loading, that is, loading only the part that will be displayed on the interface. For example, the debugging of uitabeview is 460, And the debugging of each cell is 230. In this way, at most two cells are displayed on the mobile phone interface. When you move up, the first cell leaves the interface, and the third cell will be created when it appears. Note that the key part is reached. The second cell begins to exit the interface. When the fourth cell appears, the fourth cell will not be created, but will be reused directly! That is to say, no matter you have 10 or 30 pieces of data in the uitableview, only three cells will be created to display the data!


In general, this kind of working mechanism is very reasonable. It saves resources from both the CPU and memory perspectives, but here there is a problem: this mechanism is used to display data with the same structure! In many cases, we always need to implement dynamic loading, and there is always a cell. Unlike other cells, it is used to display "loading" or "loading more. At this time, there will be overlapping images under the cell reuse mechanism!

OK. It is useless if you do not practice it. Here is a simple demonstration:

_ Objects = [[nsmutablearray alloc] init];

For (INT I = 0; I <10; I ++ ){

[_ Objects addobject: [nsstring stringwithformat: @ "text % d", I];

}

[_ Objects addobject: @ "load more"];

For (INT I = 0; I <10; I ++ ){

[_ Objects addobject: [nsstring stringwithformat: @ "text % d", I];

}

We added 21 pieces of data to an array, and the one in the middle is different, which belongs to the data structure we mentioned as inconsistent.

The cell display is written as follows:

Nsstring * _ text = [_ objects objectatindex: indexpath. Row];



// We want the line "load more" to be centered

If (! [_ Text hasprefix: @ "text"]) {

Cell. textlabel. textalignment = uitextalignmentcenter;

}


Cell. textlabel. Text = _ text;

OK, let's run:


Drag down. It seems that there is no problem. But when we drag up and down several times, the problem occurs ..


Due to the cell reuse mechanism, the "uitextalignmentcenter" attribute is gradually used by other cells. How about this swelling? At the earliest time, I searched through the Internet, and everyone said this was a problem of cell reuse. I think that, since it is reuse, I will not reuse it. So I

If (cell = nil ){

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

Cell. accessorytype = uitableviewcellaccessorydisclosureindicator;

}

Changed:

If (cell! = Nil ){

[Cell release];

}

Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier];

Cell. accessorytype = uitableviewcellaccessorydisclosureindicator;

Yes. After I completed this change, uitableview seems to have actually been working in the way I thought. In fact, it has not been wrong for a long time. (Don't ask why I don't set the text attribute to "uitextalignmentleft" after the if statement, because the actual data is much more complex than this one .) This method has always worked very well (specifically on iPhone 4). After a long time, I ran the same program on itouch, if you do not load much data, the system prompts "received memory warning .....

So I know the correct method of cell reuse:

Nsstring * _ text = [_ objects objectatindex: indexpath. Row];



Uitableviewcell * cell;

If ([_ text hasprefix: @ "text"]) {

Static nsstring * cellidentifier = @ "cell ";



Cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];

If (cell = nil ){

Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault

Reuseidentifier: cellidentifier] autoreler];

Cell. accessorytype = uitableviewcellaccessorydisclosureindicator;

}

}

Else {

Static nsstring * cellidentifier = @ "cellreuse ";



Cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];

If (cell = nil ){

Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault

Reuseidentifier: cellidentifier] autoreler];

Cell. accessorytype = uitableviewcellaccessorydisclosureindicator;

}



Cell. textlabel. textalignment = uitextalignmentcenter;

}


Cell. textlabel. Text = _ text ;,

This can be like this:

Static nsstring * normalcellidentifier = @ "normal cell ";
Static nsstring * specialcellidentifier = @ "special cell ";

Nsstring * cellidentifier;
If ([_ text hasprefix: @ "text"]) cellidentifier = normalcellidentifier;
Else cellidentifier = specialcellidentifer;

Cell = [tableview dequeuereusablecellwithidentifier: cellidentifier];
If (! Cell ){
Cell = [[[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault
Reuseidentifier: cellidentifier] autoreler];
}

Run it again and try again. Is there any error in how to drag up or down :)

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.