Mechanism

tableView call methods in UITableViewDataSource to show the data

usage

add dataSource delegation

extension ExampleViewController: UITableViewDataSource {

}
self.tableView.dataSource = self

two methods must be implemented

- (NSInteger) numberOfRowsInSection: (NSInteger) section
- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath
  1. Data Source - 定义
    @inteface ViewController() <UITableViewDataSource>
    @prperty(weak, strong> IBOutlet UITableView* tableView; 
    @end 
    self.tableView.dataSource = self;
    
  2. 方法
- (NSInteger) numberOfRowsInSection:(NSInteger) section { 
  if (section == 0) { 
      return 2; 
  }  
  if (section == 2) { 
      return 5; 
  } 
}

- (NSInteger) numberOfSectionsInTableView:(UITableView*) tableView { 
  return 3; 
}

- (UITableViewCell) tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath { 
  UITableViewCell cell = [[UITableView alloc]init];
  cell.textLabel.text = @"xxx"; 
  return cell; 
}

//section header title
- (NSString) tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section { 
  if (section == 1) { 
  return @"xxyy"; 
  } 
}

//section footer title
(NSString) talbleView:(UITableView)tableView titleForFooterInsection:(NSInteger) section { 

}

Cell 自带样式

UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle: UITableViewCellStyleSubtitle];
  //Default, Value1, Value2
  // - 自带属性
  cell.textLabel 
  cell.imageView 
  cell.detailTextLabel
  return cell

Data Model

cell model in the cell
model array in the ViewController

if we use viewModel, then we should add viewModel protocol as the cell model in the cell

Optimization

TableView optimization rules(based on reuse identification):

  • use the object pool to reuse the cell
  • once the cell should show in the screen, we reuse the cell from object pool
  • once the cell should remove from the screen, we put the cell into object pool

implementation

  • search the reusable cell on the object pool based on the reuseIdentify
  • if not, create a new cell based on the reuseIdentify
  • rewrite data to the cell we get

results matching ""

    No results matching ""