UITableView is the most commonly used control in app development and is very powerful, and it's often useful for displaying data. Before learning how to use UITableView, let's take a quick look:
1, the reuse mechanism of UITableView
UITableView's core idea is the reuse mechanism of UITableViewCell, which, for a uitableview, may need to display hundreds or thousands of cells, and if each cell is created separately, it consumes a lot of memory, in order to avoid this situation, The reuse mechanism was born.
The key to the implementation of the reuse mechanism of UITableView is the following function: 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.
Assuming that a UITABLEVIEW has 100 data to display, that is, 100 cells are required, but only 10 cells can be displayed at most once in a single screen, there is one way to create a 100cell without creating a (10+1);
When sliding the TableView, the cell that just left the view will be placed in the reuse pool, and when the next cell needs to be displayed, it will see if there is a cell in the reuse pool, and if so, remove the cell from the reuse pool and recreate the cell without it. The following code:
Static @" UITableViewCell " ; *cell = [TableView dequeuereusablecellwithidentifier:reuseindentifier]; if (! cell) { = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Reuseindentifier]; }
Create a cell of type uitableviewcellstyledefault and identify it as identifier, so that a cell is created. When the 2nd cell is needed, it is also first taken from the reuse queue, found or not, then continue to create. As you can imagine, you need to create 10 cells when you first enter this page, and their identifiers are identifier.
When we pull down tableview, we know that there will be a 11th cell. This is the time to fetch from the reuse queue, find no, continue to create the 11th cell and return, this time, when the 11th cell is fully displayed, the 1th cell is exactly gone, so that the first cell(labeled Cell1) is put into the reuse queue ".
Continue to pull, will appear the 12th cell, the same, or to first call Dequeuereusablecellwithidentifier: method, from the reuse queue to find the cell, this time there is cell1 in the queue, it will be taken out, the IF (! Cell) does not create a new cell, the cell is returned as the 12th cell, and when the 12th cell is fully displayed, the 2nd cell disappears completely into the reuse queue. As you can imagine, in 13, 14 ... and so, even though you need 100 cells, you actually just create 11 cells, which is the reuse mechanism.
2, the list of repeat confusion problem
-(UITableViewCell *) TableView: (UITableView *) tableview_ Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *reuseindentifier =@"UITableViewCell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:reuseindentifier]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseindentifier]; } UILabel*label = [[UILabel alloc]initwithframe: (cgrect) { -,Ten, $, -}]; Label.text= [NSString stringWithFormat:@"Line%d",(int) Indexpath.row]; [Cell Addsubview:label]; returncell;}
We add a label to each cell, run, drop down, and we'll find the following:
This is because every time a new cell has been used before, and then adding a label to it is added again on the original basis, so there will be a confusion as shown in the list. How to solve this phenomenon?
Method One: Remove the previously created label before creating a new label
-(UITableViewCell *) TableView: (UITableView *) tableview_ Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *reuseindentifier =@"UITableViewCell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:reuseindentifier]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:reuseindentifier]; } //Remove all child views[Cell.subviews enumerateobjectsusingblock:^ (IDobj, Nsuinteger idx, BOOL *stop) {UIView*view = (uiview*) obj; [View Removefromsuperview]; }]; //Add a new viewUILabel *label = [[UILabel alloc]initwithframe: (cgrect) { -,Ten, $, -}]; Label.text= [NSString stringWithFormat:@"Line%d",(int) Indexpath.row]; [Cell Addsubview:label]; returncell;}
Method Two: This method discards the reuse mechanism and gets the corresponding cell return each time according to Indexpath.
Method: UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:reuseindentifier];
Method of Use:UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 替换。
iOS Basics (13)--uitableview (i) Reuse mechanism