Basic use of UITableView

Basic use of UITableView Show multiple sets o...
Basic use of UITableView
Basic use of UITableViewController
Performance optimization of UITableView
Add index

Basic use of UITableView

Show multiple sets of data

#import "ViewController.h" @interface ViewController () <UITableViewDataSource> @property (nonatomic, weak) IBOutlet UITableView *tableView; @end @implementation ViewController -(void)viewDidLoad { [super viewDidLoad]; self.tableView.dataSource = self; } //Tell TableView how many groups there are in total -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 4; //Show 4 sets of data } //Tell TableView how many lines there are in section group -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(section == 0){ //How many rows are there in group 0 return 2; }else if(section == 1){ return 6; }else if (section == 2){ return 6; }else { return 1; } } //Tell tableView what each row shows (each row of tableView is UITableViewCell or its subclass) -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc] init]; if(indexPath.section == 0 && indexPath..row == 0){ cell.imageView.image = [UIImage imageNamed:@"123"]; cell.accessoryView = [[UISwitch alloc] init]; //Set right as switch cell.accessoryType = UITableViewCellAccessoryDisclosureIndictor; //Set arrow on right //Note: accessoryView has higher priority than accessoryType cell.textLabel.text = @"currency"; }else { cell.textLabel.text = @"Privacy"; } return cell; } //Set the header of each group -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if(section == 0){ return @"Japanese brands"; }else{ return @"djjhhg"; } } //Set the tail title of each group. If the title is too long, it will wrap automatically -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { if(section == 0){ return @"Japanese brands"; }else{ return @"djjhhg"; } } @end

Display single group data

//Tell TableView how many groups there are in total //If not implemented, the default is 1 group -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; //Show single set of data }

Common properties of UITableView

//Row height self.tableView.rowHeight = 44; //Default 44 //Set the head height of each group of tableView self.tableView.sectionHeaderHeight = 80; self.tableView.sectionFooterHeight = 90; self.tableView.separatorColor = [UIColor redColor]; //If it is set to * * clearColor * *, the split line can be hidden self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //Default single line self.tableView.tableHeaderView = [[UISwitch alloc] init]; //Header control of the whole table self.tableView.tableFooterView = [[UISwitch alloc] init]; //Tail control of the entire table

Common properties of UITableViewCell

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.accessoryView cell.selectionStyle = UITableViewCellSelectionStyleBlue; //Set selected color cell.selectedBackgroundView = [[UIView alloc] init]; //Set selected background picture cell.backgroundColor = [UIColor redColor]; //Set background color cell.backgroundView = [[UIView alloc] init]; //Set background priority greater than backgroundColor

contentView of cell

contentView is a sub control of cell, while other controls are actually sub controls of contentView

Proxy method of UITableView

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //Select a line to call this method } -(void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath { //Uncheck a line to call this method } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { //This method returns each group of header controls. After the method is implemented, the - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section method will not work } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { //This method sets the head height of each group. You can set the head height of each group separately } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //Set the row height of each row separately }

Basic use of UITableViewController

UITableViewController has complied with UITableViewDelegate and UITableViewDataSource protocols
Therefore, in UITableViewControll, we only need to implement data source method and proxy method.

In UITableViewController, self.view and self.tableView point to the same object.

Performance optimization of UITableView

When there are a large number of cells, we do not need to create all cells at the beginning, because not all cells will be seen by users, so cells only need to be displayed at an appropriate time. And apple has helped us realize this. Apple's implementation method is that only when the cell is to be seen by the user, the cell will be loaded.
What Apple didn't help us realize is that when we scroll forward and want to see the previous data, these cell s have been created clearly, but when we scroll forward, they will be created again. Therefore, we need to realize optimization here.
Apple's implementation principle is that every time a cell enters the field of view, it will be called once.

Solutions:
Put the cells that have been created but are no longer in view into the cache pool. Next time you create a new cell, first check whether there are cached cells in the cache pool. If there are any, just take the cells from the cache pool and use them directly.
But in the process of real use, there is not necessarily only one type of cell in the cache pool, so you need to make a mark for each type of cell.

The advantage of this is that there are only a fixed number of cells in memory, rather than unlimited cell creation.

-(void)viewDidLoad{ [super viewDidLoad]; //The cell type corresponding to the registration is UITableViewCell. When there is no corresponding cell in the cache pool, UITableView will automatically find the registered UITableViewCell type according to the identity, and then automatically create the corresponding UITableViewCell object. //The bad thing about using registration is that you can't create other styles, such as subtitle s. This method is more suitable for custom cell control. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //Whether there are recyclable cell s in the cache pool static NSString *ID = @"A"; //static only modifies the lifecycle, not the scope UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; /* if(cell == nil){ UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; cell.backgroundColor = [UIColor redColor]; } */ //Note: all cells have the same style or property, which can be unloaded from the if statement. However, if the cell style is not the same, it cannot be written in the if statement. Reusing cells can lead to confusion of data sources. //Tips, if you have else return cell; }

Add index

-(NSArray<NSString *>)sectionIndexTitlesForTableView:(UITableView *)tableView{ return @[@"A",@"B",@"C",@"D"]; //Index by group }

XMGTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

superyuan567 56 original articles published, 15 praised, 10000 visitors+ Private letter follow

8 February 2020, 08:32 | Views: 8675

Add new comment

For adding a comment, please log in
or create account

0 comments