Uitableview is a frequently used control in iOS development. It is often used to display information lists, although there may be a lot of information data, however, the resources consumed by uitableview will not increase with the increase of information displayed. This is all due to the reuse mechanism of uitableviewcell. The reuse mechanism is, as the name suggests, the mechanism of resource reuse. The following code shows how uitableviewcell is created.
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath
{
Static nsstring * cellidentifier [email protected] "normalcell ";
Uitableviewcell * cell = [tableviewdequeuereusablecellwithidentifier: cellidentifier];
If (! Cell ){
Cell = [[uitableviewcellalloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier];
}
// Processing logic
Return cell;
}
As shown above, we usually call the uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier]; statement to obtain reusable cells identified as @ "normalcell, if there is a cell identified as @ "normalcell", a cell is returned; otherwise, Nil is returned. If the return value of the preceding statement is nil, we need to create a new cell. The above method of obtaining cells is the reuse mechanism of uitableviewcell. For example, the current uitableview can display up to 8 cells at a time. We have 100 cells to display at a time. When the uitableview is initialized, it will display 1-8 cells, at this time, there is no reusable cell, so every cell needs to be created. When we drag the uitableview up, 1st cells have not completely left the visible view, and 9th cells need to be displayed, there is no reusable cell, so 9th cells need to be created. When 1st cells completely leave the visible view, they are recycled and marked as reusable, now, when you need to display 10th cells, you can obtain reusable cells. Therefore, 10th cells do not need to be created. They use the recycled 1st cells, and so on, so it is only necessary to create 9 new cells to display 100 cells.
Combined with network information and iOS sdks, you can guess the reuse mechanism of javasuitableviewcell as follows:
There should be a variable dictionary in uitableview to store reusable cells: nsmutabledictionary * reusabletablecells; When uitableview is just initialized, the content in reusabletablecells is empty. When a cell leaves the visible view, the system will store the cell to reusabletablecells Based on the reusable cell id. The implementation method is similar to the following code:
-(Void) addreuseblecell :( uitableviewcell *) Cell
{
Nsstring * reuseidentifier = cell. reuseidentifier;
Nsmutableset * reusecellset = [reusabletablecellsobjectforkey: reuseidentifier];
If (! Reusecellset ){
Reusecellset = [[nsmutablesetalloc] init];
}
[Reusecellset addobject: Cell];
[Reusabletablecells setobject: reusecellsetforkey: reuseidentifier];
}
Obtain the reusable cell method-(ID) dequeuereusablecellwithidentifier :( nsstring *) The implementation of identifier is similar to the following code:
-(ID) dequeuereusablecellwithidentifier :( nsstring *) identifier
{
Nsmutableset * reusecellset = [reusabletablecells objectforkey: identifier];
If (reusecellset. Count = 0 ){
Return nil;
}
Id anyobject = [reusecellset anyobject];
[Reusecellset removeobject: anyobject];
Return anyobject;
}
Now, the description of the reuse mechanism of uitableviewcell is complete.
Uitableviewcell reuse mechanism