Tableviewcell reuse correct understanding and usage I think

Source: Internet
Author: User

In fact, I was a little disappointed because I used Google to search for "uitableviewcell dequeuereusablecellwithidentifier" to get a bunch of articles from deep blogs. Looking at it, most of them are solving a problem:Cell display confusion during Reuse. I am not disappointed with this issue. I am disappointed with the explanations of the bloggers.

First, let's review the reuse of uitableviewcell. The basic logic is that tableview creates a screen cell at the beginning (if there are so many cells) and marks them (Identifier).IdentifierObtainRemoved Cell, Use it as a newCellRe-assigned content and then displayed. This completes cell reuse with the aim of optimizing the memory. Now, I am not doing anything about its principleFurther in-depthOnly the logic above is known. (You are welcome to discuss and point out what you think I may be wrong)

The problem is that I often see it in my work, or in the articles I mentioned earlier.See someoneIndicates a problem with reuse,The new cell sometimes displays the content of the cell that has just been removed, or the cell shows completely messy while sliding.. As a result, some writers have created some methods and explanations. For example:





Various solutions are summarized as follows:

1. UI disorder during reuse. This is a common problem!We need to use multiple methods to eliminate this reuse mechanism.(The original words of a blogger cannot speak out ...)

2. Since there is a problem with reuse, we will not reuse it. We will create as many cells as there is data.

3. reuse is re-used, but identifier uses different ones (so self-deception = #)

4. Reuse and reuse. Each time I set the subview content on the cell, I first delete all the subviews above and recreate them.

It is good to solve the problem and share the solution, but they ignore the following issues:

1. Why must IOS be reused? The reuse of optimized memory is something Apple's full-time engineer has developed. Can you deny it easily?

2. identifier is used to mark the queues of reusable cells. If you use different tags to mark cells, you can also create different tags during creation. This is self-deception, in fact, it is useless to reuse;

3. Each time you delete the object, create a subview. It seems safe. It is also the most reliable solution... But in fact, this is a loss of efficiency. The reuse mechanism is intended to optimize the memory, optimize the interface experience, and you can delete and create again to increase unnecessary workload, which is contrary to this concept.


Now I'm using a demo (here we can see the to explain how well I understand cell reuse.

First introduce the demo: I created an empty project and created three classes,

Class viewcontroller, used for the rootviewcontroller of window,

Class tableviewtestview, where tableview is created,

Class demotableviewcell, inherited from uitableviewcell, which has a label added to the cell's contentview.

Create an instance of tableviewtestview in viewcontroller, and then create tableview in tableviewtestview to implement various tableview callbacks and data sources. The cell used is demotableviewcell (please refer to the cell for details)

Correct reuse should be:

1. First, the user slides the tableview and automatically calls it each time the new cell is refreshed.

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *),You should honestly implement it

 1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 2 { 3     return self.dataSource.count; 4 } 5  6 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 7 { 8     static NSString *cellIdentifier = @"DemoTableViewCell"; 9     DemoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];10     if (!cell) {11         cell = [[DemoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];12     }13     14     [cell loadData:[self.dataSource objectAtIndex:indexPath.row]];15     16     return cell;17 }

2. Each time we call this function, as we know, we first need to identify whether there are reusable cells in the queue based on identifier. If not, create a new identifier!

3,To refresh the data, you only need to call the function in my custom cell.

-(Void) loaddata :( nsdictionary *) data;

1 [cell loadData:[self.dataSource objectAtIndex:indexPath.row]];

This function modifies the label text,You do not need to delete it first. You do not need to create a new label.

4. Where should I create the label?Create a cell in the initial place:

 1 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 2 { 3     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 4     if (self) { 5         // Initialization code 6         self.labelTest = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 24)]; 7         [self.contentView addSubview:self.labelTest]; 8     } 9     return self;10 }

In this way, when this cell is created, create labeltext and refresh the cell. You only need to call the loaddata function and use the corresponding data to refresh the label content!As long as you refresh the data correctly, the display will not be a problem!

Run the program, slide up and down, no problem!

Custom cells have more subviews. These subviews are only created when the cell is created, and the cell is refreshed when the cell is created. You only need to refresh the data by loaddata and refresh the subview display content, that's easy!

There is no need to worry about confusion, and there is no need to find various methods to circumvent the "reuse mechanism. I hope everyone will try it if there is any confusion!


Please forgive me for writing something wrong and help me to point it out. Thank you!

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: 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.