UITableView基本使用

2017-08-18  本文已影响0人  蛮荒老农

初始化

 UITableView * tableView = [[UITableView alloc]initWithFrame:frame style:UITableViewStyleGrouped];  
tableView.dataSource = self;  
 tableView.delegate = self;  
 [self.view addSubview:tableView]; 

常见属性

// 索引条内部文字颜色
self.tableView.sectionIndexColor 

// 设置索引条背景颜色
self.tableView.sectionIndexBackgroundColor 

// 行高
self.tableView.rowHeight = 100;

// 组头组尾的高
self.tableView.sectionHeaderHeight 
self.tableView.sectionFooterHeight

// 设置整个头部/尾部视图
self.tableView.tableHeaderView 
self.tableView.tableFooterView 

 // 分割线颜色(clearColor相当于取消系统分割线)
self.tableView.separatorColor;

// 设置分割线样式
self.tableView.separatorStyle;
// 告诉tableView的真实高度是自动计算的,根据你的约束来计算
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 告诉tableView所有cell的估计行高
self.tableView.estimatedRowHeight = 44
// 返回估算告诉,作用:在tablView显示时候,先根据估算高度得到整个tablView高,而不必知道每个cell的高度,从而达到高度方法的懒加载调用

常用数据源方法

// 返回行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section

// 设置cell
- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
  static NSString *CMainCell = @"Cell";    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell];    
    if (cell == nil) {  
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier: CMainCell]; 
       //cell = [[[UINib nibWithNibName:@"CellName" bundle:nil]instantiateWithOwner:self options:nil]lastObject];
       //cell = [[[NSBundle mainBundle] loadNibNamed:@"CellName" owner:nil options:nil] lastObject]; 
    } 
      return cell;  
}

#pragma mark - 代理方法
 //设置行高
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath

 // 添加每组的组头
- (UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section

// 返回每组的组尾
- (UIView *)tableView:(nonnull UITableView *)tableView viewForFooterInSection:(NSInteger)section

// 选中某行cell时会调用
- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath

// 取消选中某行cell会调用 (当我选中第0行的时候,如果现在要改为选中第1行 - 》会先取消选中第0行,然后调用选中第1行的操作)
- (void)tableView:(nonnull UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath;

// 设置UITableView的索引条,返回数组字符串集
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;

图片
上一篇下一篇

猜你喜欢

热点阅读