Solution of duplication of data in Tableviewcell

Source: Internet
Author: User

when using TableView, it is very easy to have the data duplication problem of the cell, so the solution is sorted out, later it is convenient to use, and also hope to help everyone.

the reuse of the first cell

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
     UITableViewCell *cell = [TableView dequeuereusablecellwithreuseidentifier:kidentifier forIndexPath:indexPath];
     return cell;
}

This type of multiplexing can be used in the following ways when data duplication occurs

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
     UITableViewCell *cell = [TableView dequeuereusablecellwithreuseidentifier:kidentifier forIndexPath:indexPath];
     For (UIView *view in cell.subviews) {
        if (view) {
            [view Removefromsuperview];
        }
    }
     return cell;
}

the reuse of the second cell

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
     UITableViewCell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:kidentifier];
      if (!cell) {
        cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle Reuseidentifier: Cellidentifier];
    }
     return cell;
}

When you use this method to reuse the cell, there is duplication of data, you can use the following methods to solve 1

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

      // Defines a unique identity
    static nsstring *cellidentifier = @ "Cell";
    Creating a cell instance by Indexpath each cell is a separate
    uitableviewcell *cell = [TableView Cellforrowatindexpath:indexpath];
      if (!cell) {
        cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle Reuseidentifier: Cellidentifier];
    }
     return cell;
}
2
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: ( Nsindexpath *) Indexpath {//define cell identity each cell corresponds to one's own identity nsstring *cellidentifier = [NSString stringwithformat:@] C
   Ell%ld%ld ", Indexpath.section,indexpath.row];  
      UITableViewCell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:cellidentifier]; if (!cell) {cell = [UITableViewCell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentif
    IER];
} return cell; }

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.