Ios-tableview reuse principle and attention point

Source: Internet
Author: User

What is cell reuse? #

The reuse mechanism, simply speaking, means that a row of cells is reused

Why is reuse being implemented? #

In order to display and data separation, by reusing cells to achieve the purpose of saving memory (one screen shows the number of cells is limited, when the screen scrolling, will call the method to get the new cell, and some cells will scroll to the outside of the screen).

How is cell reuse implemented? #

Through the UITableView "dequeuereusablecellwithidentifier" function to achieve, literally understand is "out of the reusable cell", in fact, simply say is a cell pool, which is placed in the cell you created before.

How is the reuse mechanism implemented? #

UITableView header files that have visiblecells and Reusabletablecells,visiblecells save the currently displayed Cells,reusabletablecells save the reusable cells.
TableView at the beginning of the show, Reusabletablecells is empty,
So TableView Dequeuereusablecellwithidentifier: return nil.
The starting cell is created by [[UITableViewCell alloc] Initwithstyle:reuseidentifier:], and the Cellforrowatindexpath is simply the number of times the maximum number of cells is called.

Note the point: #
    1. The re-extracted cell is likely to have been bundled with data or added to the child view, if necessary, to clear the need to use with the displayed data and remove the add over the child view.

    2. The principle is to avoid frequent alloc and Delloc cell objects.

    3. The key to design is to achieve a complete separation of the cell and the data.

Workaround:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  //改为以下的方法  UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

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.

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.
      After the Reloaddata call, move all cells in the Visiblecells (currently displayed cell) into Reusabletablecells, and then empty the visiblecells.
      After the Cellforrowatindexpath call, the reused cell is removed from the reusabletablecells and placed into the visiblecells.

    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 visiblecells. The old cell moved out of the Visiblecells and joined the Reusabletablecells. Then, after the refresh there is a cell to do the reuse.



Links: Https://www.jianshu.com/p/7fb91f1ce3a5



Ios-tableview reuse principle and attention point

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.