First, UITableView basic introduction
The default UITableView has 2 flavors:
- Uitableviewstyleplain (not grouped)
- uitableviewstylegrouped (grouped)
The data in the UITableView is only the concept of row, the concept of no column, uitableview each row of data is a uitableviewcell.
The type selection of the UITableViewCell is:
Copy Code code as follows:
typedef ns_enum (Nsinteger, Uitableviewcellstyle) {
Uitableviewcellstyledefault,///left display Textlabel (Detailtextlabel not shown), ImageView optional (show on the left)
UITableViewCellStyleValue1,///left side display Textlabel, right display Detailtextlabel (default blue), ImageView optional (show on the left)
UITableViewCellStyleValue2,///left Textlabel (default blue) and Detailtextlabel,imageview optional (display on the left)
Uitableviewcellstylesubtitle///top left display Textlabel, Detailtextlabel (default gray), ImageView optional (show on the left)
};
Second, Uitableviewdatasource data source
The role of the data source is to tell UITableView what data I should show
Copy Code code as follows:
#pragma mark Common Data source method
#pragma mark returns the number of groups
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
#pragma mark returns the number of rows per group
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
#pragma mark returns the cells of each row
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
#pragma mark returns each group header title name
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section
#pragma mark returns each set of tail instructions
-(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (nsinteger) Section
Calculate the number of groups-> calculate the number of rows per group-> generate a grouped index-> generate cells
Note: Cellforrowatindexpath only produces cells that are currently displayed on the interface
Third, uitableviewdelegate agent
The role of the agent is to tell UITableView how I should show and respond
Copy Code code as follows:
#pragma mark-common agent method
#pragma mark sets the height of the contents of the grouped headers
-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (nsinteger) Section
#pragma mark sets the height of each line (the height of each row can be different)
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath
#pragma mark sets the height of the contents of the packet tail
-(CGFloat) TableView: (UITableView *) TableView heightforfooterinsection: (nsinteger) Section
#pragma mark clicked on a line
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
#pragma mark sets the header view for grouping
-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section;
#pragma mark sets the tail view of a group
-(UIView *) TableView: (UITableView *) TableView viewforfooterinsection: (nsinteger) Section;
Four, UITableView Refresh list method
Copy Code code as follows:
#pragma mark, refresh the entire table.
-(void) reloaddata;
#pragma mark refreshes the specified line
-(void) Reloadrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;
#pragma mark refreshes the specified group
-(void) Reloadsections: (Nsindexset *) Sections withrowanimation: (uitableviewrowanimation) animation;
Refreshes the specified row data #pragma mark deletion
-(void) Deleterowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;
Refresh the specified row data #pragma mark is added
-(void) Insertrowsatindexpaths: (Nsarray *) indexpaths withrowanimation: (uitableviewrowanimation) animation;
Five, uitableviewcell mechanism of reuse
There is a cache pool inside the UITableView that is designed to cache UITableViewCell, because UITableView does not display all the cells at once, but instead sees the cell on the phone in the WYSIWYG way, There is an object UITableViewCell instance that exists. The specific performance is as follows:
Each time a new cell is displayed, the corresponding UITableViewCell object is removed from the cache pool for reinitialization. Create a new UITableViewCell object if it is not in the cache pool
Whenever a cell is moved out of the visible area, it is reclaimed to the cache pool
So although the data to be displayed is huge, the UITableViewCell in memory is also limited, which greatly reduces the need for memory.
Copy Code code as follows:
# pragma mark in Tableview:cellforrowatindexpath: Using the UITableView reuse mechanism in the method
Because of the frequent invocation of this method, the declaration of a cell as a static variable is advantageous to performance optimization
static NSString *cellidentifier = @ "UITableViewCellIdentifierKey1";
To cache the pool first according to the identity
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
If the cache pool is not found, recreate it and put it in the cache pool
if (!cell) {
cell = [[UITableViewCell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier];
}
Vi. UITableViewCell system with its own
We rarely use the UITableViewCell of the system itself, and the style is too rigid.
Vii. Custom Cell
Basic steps:
Custom class Xxxtableviewcell, inheriting UITableViewCell
Override-(ID) Initwithstyle:reuseidentifier: method, adding child controls
It is best to rewrite the Layoutsubview method to set the child control frame
The class Xxxtableviewcell is then created using the reuse mechanism in the UITableView proxy method Tableview:cellforrowatindexpath: And then the cell is initialized
Viii. Pattern of MVC