Analysis of the reuse mechanism of cell reuse in IOS table

Source: Internet
Author: User

Analysis of the reuse mechanism of cell reuse in IOS table

Technical Exchange new QQ Group: 414971585

After you create an instance of the Uitableviewcontroller subclass, the following paragraphs are in the code generated by the IDE:

  1. -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {
  2. static NSString *cellidentifier = [NSString stringwithformat:@"Cell"];
  3. UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
  4. if (cell = = nil) {
  5. cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier] Autorelease];
  6. }
  7. //config the cell
  8. return cell;
  9. }

This involves the reuse mechanism of tableview, in order to achieve display and data separation, the implementation of IOS TableView and does not create a TableCell for each data item. Instead of just creating a cell with the maximum number of screens to display, and then reusing the cells, the cell is configured for a separate display to achieve the goal of not compromising the display and saving content. The following is a brief analysis of its implementation principle.

  

Reuse Implementation Analytics

Viewing the UITableView header file, you will find nsmutablearray* visiablecells, and nsmutabledictnery* reusabletablecells two structures. Save the cells,reusabletablecells cells that are currently displayed in Visiablecells.

At the beginning of TableView display, Reusabletablecells is empty, then TableView Dequeuereusablecellwithidentifier:cellidentifier returns nil. The starting cell is created by [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier], And Cellforrowatindexpath just calls the maximum number of cells to display.

For example: There are 100 data, iphone one screen display up to 10 cells. The scenario at which the program initially shows TableView is:

1. Create 10 cells with [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier], and assign the same reuse identity to the cell (of course, you can specify different identities for cells of different display types). And all 10 cells are added to the Visiablecells array, and the Reusabletablecells is empty.

2. Drag the TableView down, when Cell1 completely out of the screen, and CELL11 (it is also alloc out, the reason above) is fully displayed. Cell11 joins to Visiablecells,cell1 to move out visiablecells,cell1 to join to Reusabletablecells.

3. Then drag the TableView down, because there are already values in the reusabletablecells, so when it is necessary to show that the new Cell,cellforrowatindexpath is called again, TableView Dequeuereusablecellwithidentifier:cellidentifier, return to CELL1. Cell1 joins the VISIABLECELLS,CELL1 to move out of the reusabletablecells;cell2 to move out visiablecells,cell2 join to Reusabletablecells. Cells that need to be displayed later can be reused normally.

So the whole process is not difficult to understand, but it is because of the reason: When you configure the cell, you must pay attention to the re-assignment of the retrieved cells, do not leave old data.

Some cases

During use, I noticed that the Reusabletablecells table was not updated until the screen was dragged, as well as:

1. Reloaddata, this is a very special situation. It is generally called when some data changes and needs to refresh the contents of the cell display. In the Cellforrowatindexpath call, all cells are reused. I estimate that after Reloaddata is called, all cells in the visiablecells are moved into the reusabletablecells,visiablecells empty. After the Cellforrowatindexpath call, the reuse cell is removed from the reusabletablecells and placed into the visiablecells.

2. Reloadrowsatindex, refreshes the specified indexpath. If the call is Reusabletablecells empty, then Cellforrowatindexpath is called after the new cell is created, and the new cell is added to the visiablecells. The old cell moved out of the Visiablecells and joined the Reusabletablecells. Then, after the refresh there is a cell to do reuse.

Analysis of the reuse mechanism of cell reuse in IOS table

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.