IOS tests the UITableView reuse mechanism and uses facts to speak.

Source: Internet
Author: User

IOS tests the UITableView reuse mechanism and uses facts to speak.
The UITableView reuse mechanism relies mainly on reuseIdentifier to identify and establish a queue, put the created Cell into the queue, and then directly use the Cell in the queue instead of creating a new one, this greatly improves the reusability of TableView and prevents freezing when the list slides. TableView is a beginner. I am the first control to master and use TableVIew most. Many people have mentioned the reuse mechanism in many places, but I still want to try again from my own perspective. Because the list content is too long to be directly displayed on the interface, you can only extract the printed data.

1. How is the reuse mechanism Cell created?

Many people think that only one Cell is created when it is created, and the other one is used again, but in fact this is not the case. How many cells are created, the addition of the reuse queue depends on how many cells can be accommodated on the screen during initialization (5S Simulator Test). When the Cell height is 44, 14 new cells are created. When the Cell height is 120, count: 6 and create 6 new cells.


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static int count = 0;    static NSString *celleIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celleIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celleIdentifier];        cell.selectionStyle = UITableViewCellSelectionStyleGray;        count ++;    }    if (indexPath.row < 10) {        cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];        NSLog(@"count:%d,cellheight:%f",count,cell.frame.size.height);    return cell;}


2. How is Cell retrieved when it is reused?

When a Cell with a height of 120 is used, there will be only six storage cells in the reuse queue, and up to 6th cells are included in the Code below, that is, when textLabel = 6 is used, the Cell in the reuse queue is used. According to the printed data, the init textLabel is 0, which uses the first Cell in the queue, when textLabel = 7, the init textLabel is 1, which uses the second Cell to be stored, and re-uses the first Cell after the sixth round. It can be inferred that, cell reuse follows the principle of first-in-first-out and applies it cyclically.


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static int count = 0;    static NSString *celleIdentifier = @"Cell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celleIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celleIdentifier];        cell.selectionStyle = UITableViewCellSelectionStyleGray;        count ++;    }    NSLog(@"init textLabel:%@",cell.textLabel.text);    if (indexPath.row < 10) {        cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];    }    NSLog(@"count:%d,textLabel:%@",count,cell.textLabel.text);    return cell;}


3. What is the role of reuseIdentifier?

When loading 11th cells, the Cell's celleIdentifier is replaced with @ "cell2", and the textLabel content of the 11th cells is transferred to @ "this is 20". According to this, because celleIdentifier is replaced, there is no init textLabel when loading the 11th cells, and count starts to increase from 12, it indicates that the new celleIdentifier is used to re-create a queue for tableview and put six new cells. After six rounds, the cells in the new queue are reused.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static int count = 0;    static NSString *celleIdentifier = @"Cell";    if (indexPath.row > 10) {        celleIdentifier = @"Cell2";    }    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celleIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:celleIdentifier];        cell.selectionStyle = UITableViewCellSelectionStyleGray;        count ++;    }    NSLog(@"init textLabel:%@",cell.textLabel.text);    if (indexPath.row < 10) {        cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];    }else if (indexPath.row == 11){        cell.textLabel.text = @"this is 20";    }    NSLog(@"count:%d,textLabel:%@",count,cell.textLabel.text);    return cell;}



4. Why is my data messy after I use the reuse mechanism?

In the third part of the explanation, after the first Cell, textLabel always displays null. This is because I have not performed any operations on the Cell after reuse. Generally, after reuse, the data of the Cell must be processed. If no processing is performed, in the same case as above, because the Cell textLabel In the reuse queue has no value, it is not operated after it is obtained, that is, null. Another case is to print out this is 20, because the Celld textLabel In the reuse queue has a value, if you do not perform any operations after reuse, the textLabel value of the Cell in the previous queue will be used. Therefore, when several cells are first stored in the Cell2 queue for reuse, the printed data is also this is 20.


Conclusion: Different reuseIdentifier indicates different queues. When a queue is reused, it is first-in-first-out in order, and then reused from the first round.

Related Article

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.