iOS Learning Note (4) The reuse mechanism of-uitableview
The cells in the UITableView are dynamic, and in use, the number of cells that need to be displayed on the screen is calculated based on the height of the screen (480) and the height of each cell. For example, the cell height is 90. Then 480/90 = 5 + 1, which means that up to 6 cells can be displayed on the screen.
The system creates 1 cel pools, no matter how many rows are created in the TableView, only 6 cells are placed in the pool. When a row moves out of the screen, the cell is put back in the pool, and a cell is removed from the pool when a row needs to be displayed in the screen.
The reuse mechanism requires a cellidentifier (row identifier) to differentiate between the different kinds of cells that are needed, and if the cell of the same type requires only one identifier.
You can manipulate the cell, change properties such as text, background images, but not change itself, and not convert it to another type of object. If there is this need, two or more cellidentifier should be developed.
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ staticNSString* aCellIdentifier = @"CELLIDENTIFIERl"; CustomCell*ell=(CustomCell*)[tableView] dequeueReusableCellWithIdentifier:aCellIdentifier]; if(cell == nil) { cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: aCellIdentifier] autorelease]; <br> } <br> cell.title = [NSStringstringWithFormat:@”标题 %d”,indexPath.row]; <br> returncell; <br>} |
Code Analysis:
Customcell inherits from Uitablecell.
Customcell *cell = (Customcell *) [TableView dequeuereusablecellwithidentifier:cellidentifier]; its documentation is described below: 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.
In this example, if (cell = = nil) {} The statement executes only 6 times, the first 6 pools in which no cell is taken out of the cell is nil,6 times after the cell can be removed from the pool, the condition is not directly executed after the statement.
The benefit of the reuse mechanism is obvious, regardless of how many rows the TableView has to create only the number of rows that the screen can display. Save the content. Because the buffer pool realizes the cell reuse, it avoids the repeated alloc and release.
UITableView optimization points that can be exploited at the time of development.
1. All cell-generic properties are placed in a custom cell to save overhead, such as background images.
2, if the cell only a few species, then use a different cellidentifier identification, avoid repeatedly operating the cell subview with memory for CPU.
3, the type of cell can not be too much, too much cellindentifier equivalent to the abolition of the uitableview reuse mechanism.
iOS Learning Note (4) The reuse mechanism of-uitableview