The method of the cell reuse mechanism in UITableViewCell causes the content to repeat

Source: Internet
Author: User

UITableView inherits from Uiscrollview, a scroll-based control that Apple encapsulates for us. The above is mainly a uitableviewcell, you can let UITableViewCell respond to a few click events, you can also add Uitextfield or Uitextview in the UITableViewCell, and other sub-views, Makes it possible to edit text on the cell.

The cell in the UITableView can have a lot of memory, typically by reusing the cell: by specifying a reuse identifier (reuseidentifier) for each cell, which specifies the kind of cell, when the cell rolls out of the screen, The cells that are scrolled out of the screen are placed in the reused queue, and the cells are removed from the queue for reuse when a cell is not displayed on the screen.

But for a mutable custom cell, sometimes this reuse mechanism goes wrong. For example, when a cell contains a subclass of Uitextfield and is placed in the reuse queue for reuse, if a cell that does not contain any child views is to be displayed on the screen, it will be taken out and displayed in a cell with no child views, using the cell that is reused. It's going to go wrong.

Workaround:

Method One:

The method to get the cell from-(uitableviewcell*) Dequeuereusablecellwithidentifier: (nsstring*) identifier for-(UITableViewCell *) Cellforrowatindexpath: (Nsindexpath *) Indexpath

The reuse mechanism calls the Dequeuereusablecellwithidentifier method, which means "dequeue a reusable Cell", So just swap it for cellforrowatindexpath (remove the cell only from the row of the cell you want to update), you can use no reuse mechanism, so the problem can be solved, although some space may be wasted.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellidentifier =@"Cell"; //UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; //change to the following method//Take a row exactly as Indexpath, rather than removing it from the cell reuse queueUITableViewCell *cell =[TableView Cellforrowatindexpath:indexpath]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; }    //... Other Code}

Method Two: Resolve by assigning a different reuse identifier (reuseidentifier) for each cell

The reuse mechanism is to reuse cells based on the same identifiers, and cells with different identifiers cannot be reused by each other. So we can avoid the problem of different cell reuse by setting the identifier of each cell to be different.

  

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//uniquely identify the cell with Indexpath.NSString *cellidentifier = [NSString stringWithFormat:@"cell%d%d", [Indexpath section], [Indexpath Row]]; //dequeue a reusable cellUITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; }    //... Other Code}

Method Three: Remove all child views that reuse cells

This approach is done by removing all the child views of the reused cell, resulting in a cell that has no special format for reuse by other cells.

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cellidentifier =@"Cell"; //dequeue a reusable cellUITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; }    Else    {        //Delete all child views of a cell         while([Cell.contentView.subviews lastobject]! =Nil) {[(UIView*) [Cell.contentView.subviews Lastobject] removefromsuperview]; }    }    //... Other Code}

Method Three I try to seem to have some problems, specifically how to get in the study,

The method of the cell reuse mechanism in UITableViewCell causes the content to repeat

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.