Workarounds for adding sub-views in a cell reuse overlap problem

Source: Internet
Author: User

The nature of the problem: because the child view you are adding is not implemented in a custom cell, but is based on an instance created by the system's UITableViewCell class, each time the Cellforrow method creates a cell, each time a child view is created (button , label, etc.) added, will take up a lot of memory, so the use of multiplexing method, but the problem comes, when the cell out of the interface, from the queue to be reused, where the child view of the content is not eliminated, so you will be the original base and then create a sub-view added to cover the original view , the general view is transparent so that it becomes a layer of overlay.

Workaround:

The following is an example of a label added to the cell

Add a specific view instance to the cell's Contentview

Note: How do I identify a child view in the parent view?

Step1: When adding a child view, specify the tag value (greater than 0)

Step2: In order to find this child view, the specified view can be found by invoking the Viewwithtag method of the parent view

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:@ "Cell1"];

if (Cell==nil) {

Cell=[[uitableviewcell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:@ "Cell1"];

}

Forindexpath:indexpath the index to the system for registration, if you want to create it yourself, remove it.

[Email protected] "detail. ";

Cell.imageview.image=[uiimage imagenamed:@ "Icon1.png"];

[Email protected] "helo";

Try to find it in the cell by tag value.

UILabel *label= (uilabel*) [Cell.contentview viewwithtag:1];

if (Label==nil) {//If not found then new

Label=[[uilabel Alloc]init];

Set Tag value for label

label.tag=1;

Label.textcolor=[uicolor Redcolor];

Label.font=[uifont italicsystemfontofsize:40];

Label.frame=cgrectmake (0, 0, Cell.bounds.size.width, 42);

Label.textalignment=nstextalignmentcenter;

[Cell.contentview Addsubview:label];

}

Label.text=[nsstring stringwithformat:@ "%ld", Indexpath.row];

return cell;

}

Workarounds for adding sub-views in a cell reuse overlap problem

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.