IOS controls-reuse mechanism for UITableView

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/joiningss/article/details/6702023

First, the reuse mechanism of UITableView:

The iphone reuse mechanism is a memory-saving mechanism used by Apple to achieve a large amount of data display, such as in UITableView and ScrollView. Why "reusable"??? For our project, memory control is essential, if a tableview has hundreds of cells, this memory consumption is very large, and some cells have image, such as memory resources exist, it is easy to appear memory Warning even crash out, that's not what we want to see. In this respect, TableView realizes its own management method Dequeuereusablecellwithidentifier. PS: We scrollview in some projects to show a lot of images, in the ScrollView swipe to do so, to avoid excessive memory consumption, but tableview it has implemented this method, rather than we write.

Second, the use of UITableView will appear the Cell data error problem (This situation is more when using StoryBoard)

The Apple document does not encourage us to add subview to UITableViewCell, preferably with a custom cell, to add the required Subview to the cell. There is an overlap when you use Addsubview to add views to each item. For example, the cell in UITableView, if you add a child view on the cell, will reproduce the behavior of the child attempting to overlap when using the apple reuse mechanism. Or there are two issues mentioned at the beginning. If the amount of data is not many, you can manually block out the reuse mechanism of UITableView.

Third, the key to realize the reuse mechanism of UITableView

UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];?? First, we need to understand this, what this function does, and its documentation is as follows:? Returns a reusable Table-view cell object located by its identifier. It returns a reusable Tableviewcell managed by identifier, where the focus is on the "reusable" 3 words. Let's see how it's implemented, for example, when the system starts, TableView can show how many cells, here we assume 10, at the beginning of TableView will generate 10 Tableviewcell, and to have their own tag value, assumed to be 0-9. (PS: Apple's official video also mentions, try to avoid frequent add/remove view or controls, etc.). Custom AH custom, relative to the Android space customization,) so the following method to achieve: When the tableview upward scrolling, the tag 0 cell will no longer display, and then we move the tag 0 cell to the tag 9 cell below, Reset the associated property, and then move the cell with tag 1 to the cell under tag 0 ... And so on This is called "reusable".?? However, some of the properties of the cell with the tag 0 moved at this time are still unchanged (including the previously added Subview), so there will be some nonsense bugs (so much so, isn't it a relief?) )。

Iv. How to solve the errors caused by the reuse mechanism of UITableView

Scenario One: The first use of Xib to replace the version of the story (StoryBoard) custom UITableViewCell

Key code:

* Cell = (*if (cell = = nil) {     Nsarray* nib = [[NSBundle mainbundle] loadnibnamed:@ "
    Videocell"  owner:self Options:nil];     = [nib Objectatindex:0];}

PS: Note that this can only be done using the Dequeuereusablecellwithidentifier: method instead of using the Dequeuereusablecellwithidentifier:forindexpath: method, The latter seems to belong to the story version dedicated

Scenario Two: Remove all UITableViewCell added SubView

UITableViewCell *cell =[TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil)    {= [[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier : Cellidentifier];} Nsarray*subviews = [[Nsarray alloc]initwitharray:cell.contentview.subviews];  for inch subviews) {    [Subview Removefromsuperview];}

IOS controls-reuse mechanism for UITableView

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.