iOS Tableviewcell reuse mechanism to avoid repeated display problems

Source: Internet
Author: User

The general configuration is as follows when more than the range shown in the TableView is displayed and the contents are repeated as before
This configuration is more than the contents of the page will appear repeatedly-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    //defines a unique identifier for    the static nsstring *cellidentifier = @ "Cell";    Create a cell instance with a unique identity    uitableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];    Determine null initialization  -(the previous cell will be reused if the page is pulled beyond the main page content, and will not be initialized again)    if (!cell) {        cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];    }    Simple data configuration for cell    Cell.textLabel.text = @ "text";    Cell.detailTextLabel.text = @ "text";    Cell.imageView.image = [UIImage imagenamed:@ "4.png"];        return cell;} 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.

Scenario one  by not allowing him to reuse the cell to resolve the duplicate display-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: ( Nsindexpath *) indexpath{    //define a unique identity    static nsstring *cellidentifier = @ "Cell";    Creating a cell instance from Indexpath each cell is a separate    uitableviewcell *cell = [TableView Cellforrowatindexpath:indexpath];    Determine null initialization  -(the previous cell will be reused if the page is pulled beyond the main page content, and will not be initialized again)    if (!cell) {        cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];    }    Simple data configuration for cell    Cell.textLabel.text = @ "text";    Cell.detailTextLabel.text = @ "text";    Cell.imageView.image = [UIImage imagenamed:@ "4.png"];        return cell;}


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

Scenario two  also by not allowing him to reuse the cell to resolve the duplicate display is different for each cell corresponding to an identity-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{    //define cell ID  each cell corresponds to one of its own identity    nsstring *cellidentifier = [NSString stringwithformat:@ "Cell%ld%ld", Indexpath.section,indexpath.row];    Create a cell instance with different identities    uitableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];    Determine null initialization  -(the previous cell will be reused if the page is pulled beyond the main page content, and will not be initialized again)    if (!cell) {        cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier];    }    Simple data configuration for cell    Cell.textLabel.text = @ "text";    Cell.detailTextLabel.text = @ "text";    Cell.imageView.image = [UIImage imagenamed:@ "4.png"];        return cell;}


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
Scenario three when the page pulls the need to display the new data, the last cell to delete it can be customized cell this scheme to avoid repeated display, and reuse the cell relative memory management is the best solution before the two relatively consumption of memory-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//define unique identity static NSString *cellid    Entifier = @ "Cell";       Create a cell instance with a unique identity uitableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; The decision to initialize is null-(when the pull page displays more than the main page content, the previous cell is reused instead of being initialized again) if (!cell) {cell = [[UITableViewCell alloc]initwithstyl    E:uitableviewcellstylesubtitle Reuseidentifier:cellidentifier]; } else//when the page is pulled, when the cell exists and the last one exists to delete it, a unique cell is configured to avoid {while ([Cell.contentView.subviews Lastobje        CT] = nil) {[(UIView *) [cell.contentView.subviews lastobject] removefromsuperview];    }}//To cell for simple data configuration Cell.textLabel.text = @ "text";    Cell.detailTextLabel.text = @ "text";        Cell.imageView.image = [UIImage imagenamed:@ "4.png"]; return cell;}
The above are personal understanding, I am also a rookie, there is no understanding of the place I hope you point out, but also hope to be able to help you a certain!! Thank you!

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.