iOS development experience Sharing: UITableViewCell reuse issues

Source: Internet
Author: User

Many friends think UITableViewCell reuse problem is difficult to deal with, hundred think not its solution, even a lot of friends themselves pondering for a long time also do not understand. Now share some of the personal experience, I hope to be helpful, if there are good ideas or have different views can be put forward, let us share the progress together, knowledge only in the context of sharing can realize its maximum value. Okay, cut the crap and head straight to the subject. List two scenes to compare, perhaps Tableviewcell's reuse is very clear. This article comes from an unlimited number of connected learners.

Example 1:

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

static NSString *cellidentifier = @ "Cell1";

UITableViewCell *cell =[tableview Dequeuereusablecellwithidentifier:cellidentifier];

if (cell = = nil) {

cell = [UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];

UILabel *labeltest = [UILabel alloc]init];

[Labeltest Setframe:cgrectmake (2, 2, 80, 40)];

[Labeltest Setbackgroundcolor:[uicolor Clearcolor];

[Labeltest settag:1];

[Cell contentview]addsubview:labeltest];

}

UILabel *label1 = (uilabel*) [cell viewwithtag:1];

[Label1 settext:[self.tests ObjectAtIndex:indexPath.row];

return cell;

}

Example 2:

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (nsindexpath*) indexpath{

static NSString *cellidentifier = @ "Cell1";

UITableViewCell *cell =[tableview Dequeuereusablecellwithidentifier:cellidentifier];

if (cell = = nil) {

cell = [UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];

}

UILabel *labeltest = [UILabel alloc]init];

[Labeltest Setframe:cgrectmake (2, 2, 80, 40)];

[Labeltest Setbackgroundcolor:[uicolor Clearcolor];//The background is set to transparent, so that you can see the label superimposed on the cell in the back.

[Labeltest settag:1];

[Cell contentview]addsubview:labeltest];

[Labeltest settext:[self.tests ObjectAtIndex:indexPath.row];

return cell;

}

When you swipe up and down the TableView, you will see the difference, the first program interface will not be abnormal, but the second is not, there will be a font overlay phenomenon, in fact, more accurate is the overlay of multiple labels. Why, because when the tableview is refreshed, if that location already has a ready-made cell, it will not re-request the resource to generate a new cell, but instead reuse the original cell. So for the first, the idea of the code is to generate a cell for the first time when the cell does not exist, to define the cell style, and later, whether it is refreshing or re-requesting it, it is just reusing the generated cell. The second idea is that when the cell does not exist, the request to generate a cell, and then add a label to the cell, refresh, will be reused the existing cell, but will be repeated to add the label, resulting in overlapping phenomenon.

A similar question back and forth troubled me many times, I have not resolved to thoroughly understand, every time is muddle, as long as the program is best adjusted, OK. Today, I met a similar problem, and finally, I hope it will not be entrap again.

The above share is personal experience, I hope we have good knowledge, there is dry goods also shared out, let us learn together, progress together. Of course, I hope that I share these small knowledge can help everyone, to help everyone is my greatest satisfaction. Author information: Teacher from infinite interconnection Chen

iOS development experience Sharing: UITableViewCell reuse issues

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.