iOS Tableviewcell reuse mechanism to avoid repeated display problems

Source: Internet
Author: User

1//This configuration is more than the page display of the content will be repeated 2-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 3 {4     //define unique identifier 5     static nsstring *cellidentifier = @ "cell"; 6     //Create cell instance by unique ID 7     UITableView Cell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; 8     //Judge NULL for initialization  -(when the pull page displays more than the main page content, the previous cell is reused instead of being initialized again) 9     if (!cell) {ten         cell = [[UITableViewCell Alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];11     }12     //To cell For simple data configuration,     Cell.textLabel.text = @ "text";     Cell.detailTextLabel.text = @ "text";     Cell.imageView.image = [UIImage imagenamed:@ "4.png"];16     +     return cell;18}

The following 3 scenarios can be used to resolve

Scenario one cancels the cell reuse mechanism, and creating a cell via Indexpath will resolve the recurring display problem but it's a bit more tight than the big data.

1//Plan one  by not allowing him to reuse the cell to resolve the duplicate display 2-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: ( Nsindexpath *) Indexpath 3 {4     //define unique identity 5     static nsstring *cellidentifier = @ "cell"; 6     //Create a cell instance via Indexpath Each cell is a separate 7     uitableviewcell *cell = [TableView Cellforrowatindexpath:indexpath]; 8     //judgment NULL for initialization  -- (The previous cell will be reused when the page is pulled more than the main page content, and will not be initialized again) 9     if (!cell) {ten         cell = [[UITableViewCell alloc]initwithstyle: Uitableviewcellstylesubtitle reuseidentifier:cellidentifier];11     }12     //cell for simple data configuration     Cell.textLabel.text = @ "text";     Cell.detailTextLabel.text = @ "text";     cell.imageView.image = [UIImage imagenamed:@ "4.png"];16     +     return cell;18}

Scenario two allows each cell to have a corresponding identity this will also make the cell unusable, so it will not be repeated display content more memory consumption is also more than a similar

1//Program two  also by not allowing him to reuse the cell to resolve the duplicate display is different for each cell corresponding to an identity 2-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 3 {4     //define cell ID  each cell corresponds to one of its own identity 5     NSString * Cellidentifier = [NSString stringwithformat:@ "Cell%ld%ld", Indexpath.section,indexpath.row]; 6     //Create a cell instance with different identities 7     UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; 8     //Judge NULL for initialization  --(When the pull page displays more than the main page content, the previous cell is reused instead of being initialized again) 9     if (!cell) {ten         cell = [[UITableViewCell Alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];11     }12     //To cell For simple data configuration,     Cell.textLabel.text = @ "text";     Cell.detailTextLabel.text = @ "text";     Cell.imageView.image = [UIImage imagenamed:@ "4.png"];16     +     return cell;18}

Scenario three only the last display of the cell content is not empty, and then all its sub-views are deleted, the same as the cell alone and then with the new data to resolve the duplicate display

 1//Scenario three when the page pulls the need to display the new data, the last cell is deleted there can be custom cell this scheme to avoid repeated display, and reuse the cell relative memory management is the best solution before the two relative consumption of memory 2-(UITableViewCell * ) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 3 {4//define unique identity 5 static Nsstri ng *cellidentifier = @ "Cell"; 6//Create a cell instance with a unique identity 7 uitableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];  8 9//Judge NULL for initialization-(when the pull page displays more than the main page content, the previous cell is reused and not initialized again) (!cell) {one cell = [[UITableViewCell Alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];12}13 else//When the page pulls when the cell exists and the most             After one exists to delete it comes out a unique cell we are doing the data configuration to avoid the ([Cell.contentView.subviews lastobject]! = nil) {16      [(UIView *) [cell.contentView.subviews lastobject] removefromsuperview];17}18}19//Simple data configuration for cell 20 Cell.textLabel.text = @ "text"; cell.detailTextLabel.text = @ "text"; cell.imageView.image = [UIImage imagenamed:@ "4.png"];23 return cell;25} 

iOS Tableviewcell reuse mechanism to avoid repeated display problems

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.