(五)UITableView的用法一

简介: (五)UITableView的用法一

一、表视图的介绍


1、表视图和其他视图一样,它是iOS中最重要的试图,很多应用程序都会使用到,表只是一个容器,要想显示内容,必须提供信息。


2、表试图里面可以放很多行信息,使用表视图可以显示一个单元格列表,每个单元格列表都包含大量的信息,但仍然是一个整体。并且可以将表视图划分为多个区(section),以遍从视觉上将信息分组。表视图控制器是一种只能显示表视图的标准视图控制器,可以在表视图占据整个视图时使用这种控制器。


3、表视图的两种风格(或者说外观)

1)普通风格(或者叫无格式,它不像分组表那样在视觉上将各个区分开,但通常带可触摸的索引(类似于通讯录),有时又称他们为索引表)
    UITableViewStylePlain(段里面较多数据可以保持段头保持不动)
  2)分组风格(分组)
    UITableViewStyleGrouped
  3)UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。

二、表视图的基本结构


1、表视图有表头、表尾、中间一连串单元格试图组成 (表头,设置单元格视图,表尾)

1)设置表头    tableHeaderView (其实就是建一个label)

例如 :给tableView设置表头
UILabel *headerLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 60)];
headerLable.textAlignment = NSTextAlignmentCenter;//表头设置在中间
headerLable.text = @"五期联系人";
headerLable.backgroundColor = [UIColor cyanColor];
tableView.tableHeaderView = headerLable;

2)设置单元格试图(每个单元格都有独特的标示符,也成为重标示符,(reuse)用于在编译时引用单元格;配置表视图时,必须使用这些标示符)

UITableViewCell,单元格也可以分段显示,每一段都可以通过代理设置段头和段尾

3)设置表尾     tableFooterView

例如:给tableView设置表尾
UILabel *footterLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 60)];
footterLable.textAlignment = NSTextAlignmentCenter;
footterLable.text = @"未来由我创,爱拼才会赢.";
footterLable.backgroundColor = [UIColor clearColor];
tableView.tableFooterView = footterLable;
  1. tableView的常用属性和方法(每个属性都有举例)如果是建立的UITabelView 表达时用  它的对象加 属性名字,如果是基于UITableViewController,则运用属性直接用self.加属性名字

1.   设置表视图分割线风格
      @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;
     例如:
      self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
      self.tableView.separatorColor = [UIColor cyanColor];
     2. 设置表视图分割线颜色,默认标准灰色
     @property(nonatomic,retain) UIColor *separatorColor;
     例如:tableView.separatorColor = [UIColor redColor];
     3.设置表视图的头部视图
     @property(nonatomic,retain) UIView *tableHeaderView;
     例如:给tableView设置表头
     UILabel *headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
     headerLabel.textAlignment = NSTextAlignmentCenter;
     headerLabel.text = @"宁做鸡头不做凤尾";
     headerLabel.textColor = [UIColor whiteColor];
     headerLabel.backgroundColor = [UIColor clearColor];
     tableView.tableHeaderView = headerLabel;
     4. 设置表视图的尾部视图
     @property(nonatomic,retain) UIView *tableFooterView;
     例如: 给tableView设置表尾
     UILabel *footerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
     footerLabel.textAlignment = NSTextAlignmentCenter;
     footerLabel.text = @"爱咋咋地";
     footerLabel.textColor = [UIColor whiteColor];
     footerLabel.backgroundColor = [UIColor clearColor];
     tableView.tableFooterView = footerLabel;
     5.设置表视图单元格的行高
     @property(nonatomic) CGFloat rowHeight;
     例如:
         tableView.rowHeight = 100;
     6.设置表视图背景
        @property(nonatomic, readwrite, retain) UIView *backgroundView
    例如:
          tableView.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"apple.jpg"]];//后面是图片名字
     7.刷新表视图单元格中数据
        - (void)reloadData;
      8.显示指示条
        showsVerticalScrollIndicator
     9 设置表视图section头部行高
        @property(nonatomic) CGFloat sectionHeaderHeight;
       -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
        {
         return 40;
        }
     10. 设置表视图section尾部部行高
        @property(nonatomic) CGFloat sectionFooterHeight
       -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
        {
           return 40;
        }

三、单元格的显示

1、单元格的位置表示

NSIndexPath:能表示当前cell是tableView的第几段第几行

2、单元格的创建(必须有一个标识符)

UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

cell的样式

1)UITableViewCellStyleDefault
       左侧显示textLabel,imageView显示在最左边
     2)UITableViewCellStyleValue1
       左侧显示textLabel、右侧显示detailTextLabel,imageView显示在最左边
     3)UITableViewCellStyleValue2
        左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选
     4)UITableViewCellStyleSubtitle
        左上方显示textLabel,左下方显示detailTextLabel,imageView显示在最左边
    cell的辅助图标 accessoryType (accessory |əkˈsesəri| 辅助,附加的)
     1) 不显示任何图标
        UITableViewCellAccessoryNone, 
     2) 跳转指示图标  
        UITableViewCellAccessoryDisclosureIndicator
     3) 内容详情图标和跳转指示图标
        UITableViewCellAccessoryDetailDisclosureButton
     4) 勾选图标
         UITableViewCellAccessoryCheckmark
     5) 内容详情图标
         UITableViewCellAccessoryDetailButton 
     5) 自定义辅助图标
         accessoryView属性

3、cell的常用属性

1)设置cell的背景试图
      backgroundView
   2)设置选中的cellbei的背景图片
      selectedBackgroundView
      cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tableCell"]];
   3) 设置选中时的样式
      selectionStyle

4.UItableView表中的每一行都由一个UItableViewCell表示可以用一个图像,一些文本,一个可选择的辅助图标来配置每个UItableViewCell对象,UItableViewCell为每个cell定义了如下的所示的属性

1). textLable:Cell的主文本标签(一个UILable的对象)
      2). detailTextLable :cell的二级文本标签,当需要添加额外细节时(一个UILable对象)
      3) . imageView :一个用来装载图片的图片视图

5.UItableView 必须实现的3个核心方法(段数,行,建立一个cell)

1).段的数量,返回表视图划分多少个分区(这个方法可以分段显示或者单个列表显示我们的数据)不写代表一行
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2).设置某段行的数量 //返回给定分区有多少行,分区编号从0开始(不同的分段返回不同的行数可以用switch来做,如果是单个列表,就返回单个想要的函数即可)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3).- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath//返回一个经过正确配置的单元格对象,用于显示在表视图的指定位置(通过我们索引的路径section和row来确定)

四、代理方法(UITableViewDelegate)


1、一般是处理表视图基本样式(单元格高度)以及捕捉选中单元格事件

2、常用代理方法(下面的返回值都是数字)

1)配置行高
  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
 2)设置section 头部、尾部视图的高度(段头,段尾)
  - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
  - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
 3)自定义section头部、尾部视图,注意:需要指定高度
  1.- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
例如:
//自定义段头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
 NSString *title = dataArray[section][@"title"];
label.text = title;
label.backgroundColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;//把自定义的段头段位放到中间
return label;
}
//自定义段尾
  - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
NSString *title = dataArray[section][@"trail"];
label.text = title;
label.backgroundColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
return label;
}
 4)用户单击单元格中辅助按钮时,调用该方法
  - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
 5)用户单击单元格,调用该方法(五六很像,大家要区分开)
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
 6)取消选中单元格时,调用该方法
  - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
 7)设置单元格编辑样式
  - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
  8).创建lable时带的一些属性
    //给label指定内容
    cell.textLabel.text = self.dataArray[indexPath.row];
     //给label的内容设置字体和大小
     cell.textLabel.font = [UIFont fontWithName:self.dataArray[indexPath.row] size:20];
     cell.textLabel.textColor = [UIColor whiteColor];
     cell.backgroundColor = [UIColor clearColor];
     9).懒加载
     当用到某一个对象的时候,才创建这个对象
   - (NSArray *)dataArray
  {
   if (_dataArray == nil) {
    _dataArray = [NSArray array];
      }
     return _dataArray;
   }


目录
相关文章
|
3月前
|
开发者 iOS开发
介绍 UITableView 和 UICollectionView,它们的区别是什么?
介绍 UITableView 和 UICollectionView,它们的区别是什么?
58 0
|
Swift
swift创建tableview
tableview懒加载;添加到view中,遵守协议,注册cell
120 0
UITableView的创建
UITableView的创建
75 0
|
iOS开发
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
iOS UITableViewCell嵌套CollectionView,tableview和collectionview同时滑动bug修复
922 0
|
存储 iOS开发 搜索推荐