iOS pro-Test UITableView reuse mechanism, speak with facts.

Source: Internet
Author: User

UITableView reuse mechanism mainly relies on reuseidentifier to identify, in order to establish a queue, the built cell into the queue, and then directly use the cell in the queue, no longer new, greatly enhance the reusability of TableView, While sliding the list, there is no problem with the card. TableView is basically a novice must learn, my first mastery of the control, and the most used is tableview, re-use mechanism there are many places many people have spoken, but I still want to from their own perspective under the BB. Because the list content is too long, it is inconvenient to directly on the interface, so only the printed data is taken out.

1. How is the reuse mechanism cell created?

Many people think that the cell is only new when the new, the others are reused that one, but in fact, it is not the case, the number of new cells, and join the reuse queue is based on the number of cells in the screen can be initialized, (5S simulator test) tested cell high 44 o'clock, Create a new 14 cell. When cell height is 120, count:6, new 6 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 <) {        Cell.textLabel.text = [NSString stringwithformat:@ "%ld", (long) indexpath.row];        NSLog (@ "count:%d,cellheight:%f", count,cell.frame.size.height);    return cell;}


2, cell reuse is how to take out?

When using a 120-height cell, there will be only 6 stored cells in the reuse queue, in the code below to the 6th cell, Textlabel = = 6 o'clock used to start using the cell in the reuse queue, the printed data shows that the Init Textlabel is 0, This is using the first queued queue Cell,textlabel = = 7 o'clock, init Textlabel is 1, using the second cell that was deposited, and then using the first stored cell after the completion of the sixth round, it can be inferred that Cell reuse is adhering to the principle of FIFO, recycling.


-(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 <) {        Cell.textLabel.text = [NSString stringwithformat:@ "%ld", (long) indexpath.row];    }    NSLog (@ "count:%d,textlabel:%@", count,cell.textlabel.text);    return cell;}


3, the role of Reuseidentifier?

When the 11th cell is loaded, the cell's celleidentifier is replaced by the @ "Cell2" and the 11th cell's Textlabel content is transferred @ "This is 20", according to the Since the celleidentifier was replaced, the 11th cell was loaded without init Textlabel, and Count began to increment up to 12, Note Using the new celleidentifier allows TableView to re-build a queue and put in 6 new cells, and then reuse the cell in the new queue after 6 rounds.

-(UITableViewCell    *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath {static int count = 0;    static NSString *celleidentifier = @ "Cell";    if (Indexpath.row >) {celleidentifier = @ "Cell2";    } UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:celleidentifier]; if (cell = = nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellei        Dentifier];        Cell.selectionstyle = Uitableviewcellselectionstylegray;    Count + +;    } NSLog (@ "init textlabel:%@", cell.textLabel.text);    if (Indexpath.row <) {Cell.textLabel.text = [nsstring stringwithformat:@ "%ld", (long) indexpath.row];    }else if (Indexpath.row = =) {Cell.textLabel.text = @ "This is 20";    } NSLog (@ "count:%d,textlabel:%@", Count,cell.textlabel.text); return cell;} 



4. Why did my data get messed up after using the reuse mechanism?

At the 3rd time, after the 11th cell, Textlabel always shows null, because I did not operate on the cell after reuse, generally after using reuse, the cell data needs to be processed. If there is no processing, one situation is like the above, because the textlabel of the cell inside the queue is no value, after being taken out or not, that is null. There is also a case of printing out this is 20, because the reuse queue inside the celld of the Textlabel inside there is a value, remove the reuse after the use of the cell before the value of the Textlabel, So there are several subsequent reuse of the first stored cell in the CELL2 queue, and the printed data is also this is 20.


Summary: Different reuseidentifier represent different queues, re-first-out in queue order when reused, and then re-reused from the first after a round.

iOS pro-Test UITableView reuse mechanism, speak with facts.

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.