IOS TableView Cell reuse mechanism and common Code of TableView

Source: Internet
Author: User


After creating an instance of the UITableViewController subclass, the code generated by IDE contains the following section:

[Cpp] view plaincopy-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
Static NSString * CellIdentifier = [NSString stringWithFormat: @ "Cell"];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil ){
Cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
}
// Config the cell
Return cell;
}
The reuse mechanism of TableView is involved here. To achieve display and data separation, the implementation of IOS tableView does not create a tableCell for each data item. Instead, create the largest number of cells that can be displayed on the screen, and then reuse these cells to separately display the cells, so that the display effect is not affected, in addition, it can fully save content. The following briefly analyzes its implementation principles.

 

  

Reuse Implementation Analysis

View the UITableView header file and find the NSMutableArray * visiableCells and NSMutableDictnery * reusableTableCells structures. In visiableCells, save the currently displayed cells, and reusableTableCells save reusable cells.

When TableView is displayed, reusableTableCells is empty, tableView dequeueReusableCellWithIdentifier: CellIdentifier returns nil. The initial cell is created through [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier], and cellForRowAtIndexPath is only the number of times that the maximum number of cells is displayed.

For example, if there are 100 data records, the iPhone can display up to 10 cells on a single screen. When the program first displays TableView:

1. use [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] to create a cell 10 times and specify the same reuse ID for the cell (of course, you can specify different identifiers for cells of different display types ). All 10 cells are added to the visiableCells array, and reusableTableCells is empty.

2. Drag tableView down. When cell1 is completely removed from the screen and cell11 (it is also alloc, the same as above) is completely displayed. Cell11 is added to visiableCells, cell1 is removed from visiableCells, and cell1 is added to reusableTableCells.

3. Drag tableView down. Because there is already a value in reusableTableCells, tableView dequeueReusableCellWithIdentifier: CellIdentifier and cell1 is returned when you need to display a new cell and cellForRowAtIndexPath. Cell1 is added to visiableCells, cell1 is removed from reusableTableCells, cell2 is removed from visiableCells, and cell2 is added to reusableTableCells. Then the Cell to be displayed can be reused normally.

Therefore, the entire process is not difficult to understand, but it is precisely for this reason: When configuring the Cell, you must pay attention to re-assigning values to the reused cell to avoid legacy data.

Some situations

During usage, I noticed that the reusableTableCells table is not updated only when the screen is dragged:

1. reloadData, which is special. This is generally called when some data changes and the content displayed by the cell needs to be refreshed again. In the cellForRowAtIndexPath call, all cells are reused. I estimate that after reloadData is called, all cells in visiableCells will be moved into reusableTableCells and visiableCells will be cleared. After cellForRowAtIndexPath is called, obtain the reuse cell from reusableTableCells and put it into visiableCells.

2. reloadRowsAtIndex: refresh the specified IndexPath. If reusableTableCells is empty during the call, after cellForRowAtIndexPath is called, a new cell is created and the new cell is added to visiableCells. Remove the old cell from visiableCells and add it to reusableTableCells. As a result, the cell is reuse after the refresh.

 


Common code of TableView


-Create UITableView

DataTable = [[UITableView alloc] initWithFrame: CGRectMake (0, 0,320,420)];
[DataTable setDelegate: self];
[DataTable setDataSource: self];
[Self. view addSubview: DataTable];
[DataTable release];
 
2. Description of UITableView Methods
 
// Total number of sections
-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {
Return TitleData;
}
 
// Section Titles
// The title displayed for each section
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {
Return @"";
}
 
// Specify the number of partitions. The default value is 1.
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {
Return 4;
}
 
// Specify the number of rows in each partition. The default value is 1.
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {
}
 
// Draw a Cell
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
Static NSString * SimpleTableIdentifier = @ "SimpleTableIdentifier ";
 
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
If (cell = nil ){
Cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
ReuseIdentifier: SimpleTableIdentifier] autoreler];
}
Cell. imageView. image = image; // The image when no cell is selected
Cell. imageView. highlightedImage = highlightImage; // The image after the cell is selected
Cell. text = //.....
Return cell;
}
 
// Line indent
-(NSInteger) tableView :( UITableView *) tableView indentationLevelForRowAtIndexPath :( NSIndexPath *) indexPath {
NSUInteger row = [indexPath row];
Return row;
}
 
// Change the Row Height
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {
Return 40;
}
 
// Locate
[TopicsTable setContentOffset: CGPointMake (0, promiseNum * 44 + Chapter * 20)];
 
// Return the selected cell
NSIndexPath * ip = [NSIndexPath indexPathForRow: row inSection: section];
[TopicsTable selectRowAtIndexPath: ip animated: YES scrollPosition: UITableViewScrollPositionNone];
 
[TableView setSeparatorStyle: UITableViewCellSelectionStyleNone];
 
// Select the Cell to respond to the event
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {
[TableView deselectRowAtIndexPath: indexPath animated: YES]; // The selected reversed color disappears immediately.
}
 
// Determine the selected row (to prevent the first row from being selected)
-(NSIndexPath *) tableView :( UITableView *) tableView willSelectRowAtIndexPath :( NSIndexPath *) indexPath
{
NSUInteger row = [indexPath row];
If (row = 0)
Return nil;

Return indexPath;
}
 
// Indicates whether the del button is displayed on the cell.
-(BOOL) tableView :( UITableView *) tableView canEditRowAtIndexPath :( NSIndexPath *) indexPath {
}
 
// Edit the status
-(Void) tableView :( UITableView *) tableView commitEditingStyle :( UITableViewCellEditingStyle) editingStyle
ForRowAtIndexPath :( NSIndexPath *) indexPath
{
}
[TopicsTable setContentSize: CGSizeMake (0, controller. promiseNum * 44)];
// Add an index table to the right
-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {
}
// Return the Section title.
-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {
}
// Del button content during custom stroke
-(NSString *) tableView :( UITableView *) tableView
TitleForDeleteConfirmationButtonForRowAtIndexPath :( NSIndexPath *) indexPath
// Jump to the specified row or section
[TableView scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: 0 inSection: 0] atScrollPosition: UITableViewScrollPositionBottom animated: NO];
3. Create UILable multi-line display on UITableViewCell
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {
Static NSString * CellIdentifier = @ "Cell ";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil ){
Cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier] autorelease];
UILabel * Datalabel = [[UILabel alloc] initWithFrame: CGRectMake (10, 0,320, 44)];
[Datalabel settag: 100];
Datalabel. autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[Cell. contentView addSubview: Datalabel];
[Datalabel release];
}
UILabel * Datalabel = (UILabel *) [cell. contentView viewWithTag: 100];
[Datalabel setFont: [UIFont boldSystemFontOfSize: 18];
Datalabel. text = [data. DataArray objectAtIndex: indexPath. row];
Cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Return cell;
}
// Select the cell color
Typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle
// Cell button format on the right
Typedef enum {
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // blue button w/chevron. tracks
UITableViewCellAccessoryCheckmark // checkmark. doesn' t track
} UITableViewCellAccessoryType
// Whether to add a line break
Typedef enum {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine
} UITableViewCellSeparatorStyle // change the line feed color
TableView. separatorColor = [UIColor blueColor];

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.