iOS开发之高级视图—— UITableView(一)简单例子

来源:互联网 发布:游戏源码是什么 编辑:程序博客网 时间:2024/05/22 02:16

  表视图继承自UIScrollView,这样的继承关系使得表视图可以实现上、下滚动。

     UITableView需要实现的两个协议如下:

       UITableViewDatasource:实例化表视图时,必须采用该方法来实现数据源的配置
       UITableViewDelegate:表视图的委托方法,一般用于处理表视图的基本样式以及捕捉选中单元格选中事件


    表视图的结构:

         表视图由头部、尾部视图,中间有一连串的单元格视图

         表视图的头部由tableHeaderView属性设置,尾部视图通过tableFooterView属性设置

        分组表格由一系列的 分区 视图组成,每一个分区又包含一个连续的单元格

       每个分区视图也由头部视图和尾部视图,通过委托方法代理

   cell的使用:

       首先定义一个标示符

      其次,检查表视图中是否存在闲置的单元格,如果有取出来,没有则重新创建

     

     例子一——简单表格

    

   ViewController.m



[objc] view plain copy
  1. //  
  2. //  ViewController.m  
  3. //  UITableViewDemo  
  4. //  
  5. //  Created by Apple on 16/5/24.  
  6. //  Copyright © 2016年 Apple. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. NSArray* cityList;  
  16.   
  17. @implementation ViewController  
  18.   
  19. - (void)viewDidLoad {  
  20.     [super viewDidLoad];  
  21.       
  22.     [self.view setBackgroundColor:[UIColor redColor]];  
  23.       
  24.     //创建一个数组,存储需要显示的数据  
  25.     cityList = @[@"北京",@"上海",@"天津",@"广州",@"深圳",@"杭州",@"长沙",@"郴州"];  
  26.     //创建UITableView对象  
  27.     UITableView* tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];  
  28.       
  29.     UILabel* headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(00self.view.frame.size.width44.0)];  
  30.     [headerLabel setText:@"城市列表"];  
  31.     headerLabel.textColor = [UIColor blackColor];  
  32.     headerLabel.backgroundColor = [UIColor cyanColor];  
  33.     headerLabel.textAlignment = NSTextAlignmentCenter;  
  34.     //设置UITableView的页眉控件  
  35.     [tableView setTableHeaderView:headerLabel];  
  36.       
  37.     UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0032030)];  
  38.     [footerLabel setText:@"以上城市房价太高"];  
  39.     //设置UITableView页脚控件  
  40.     [tableView setTableFooterView:footerLabel];  
  41.       
  42.     //设置行cell高(默认44px)  
  43.     [tableView setRowHeight:50];  
  44.     //设置分割线颜色  
  45.     [tableView setSeparatorColor:[UIColor redColor]];  
  46.     //设置分割线风格  
  47.       
  48.     /** 
  49.      *  UITableViewCellSeparatorStyleNone 不使用分割线 
  50.      UITableViewCellSeparatorStyleSingleLine 使用分割线 
  51.      UITableViewCellSeparatorStyleSingleLineEtched 在有组的情况使用分割线 
  52.      */  
  53.     [tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];  
  54.     // 设置UITableView的背景颜色  
  55.     [tableView setBackgroundColor:[UIColor lightGrayColor]];  
  56.       
  57.     // 设置数据源代理,必须实现协议UITableViewDataSource中的相关方法  
  58.     tableView.delegate = self;  
  59.     tableView.dataSource = self;  
  60.       
  61.     [self.view addSubview:tableView];  
  62.       
  63.       
  64. }  
  65.   
  66. #pragma mark -UITableViewDataSource  
  67.   
  68. // 返回表格分区数,默认返回1  
  69. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  70. {  
  71.     return 1;  
  72. }  
  73. // 返回每组头标题名称  
  74. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  75. {  
  76.     return @"分区开始";  
  77. }  
  78.   
  79. //  返回每组尾部说明  
  80. -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{  
  81.     return @"分区结束";  
  82. }  
  83.   
  84. // @required  
  85. // 提供tableView中的分区中的数据的数量  
  86. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  87.       
  88.     return [cityList count];  
  89. }  
  90.   
  91. // 为表格行定义一个静态字符串作为可重用标识符,在UITableView的cell缓存池当中所有的cell的标示符都是刚定义的cellID,因为重用时无所谓获取哪一个cell,只要是cell就可以  
  92. static NSString* cellID = @"cellID";  
  93.   
  94. // @required  
  95. // 返回每行的单元格,提供 tableView 中显示的数据  
  96. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  97.     // 根据cellID从可重用表格行的队列中取出可重用的一个表格行UITableViewCell对象  
  98.     UITableViewCell* tableViewCell = [tableView dequeueReusableCellWithIdentifier:cellID];  
  99.     // 如果取出的表格行为nil  
  100.     if (tableViewCell == nil) {  
  101.         //创建一个UITableViewCell对象,并绑定到cellID  
  102.         tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];  
  103.     }  
  104.     // 将单元格的边框设置为圆角  
  105.     tableViewCell.layer.cornerRadius = 12;  
  106.     tableViewCell.layer.masksToBounds = YES;  
  107.     //UITableView声明了一个NSIndexPath的类别,主要用 来标识当前cell的在tableView中的位置,该类别有section和row两个属性,section标识当前cell处于第几个section中,row代表在该section中的第几行。  
  108.     // 从IndexPath参数获取当前行的行号  
  109.     NSUInteger rowNo = indexPath.row;  
  110.     // 取出cityList中索引为rowNo的元素作为UITableViewCell的文本标题  
  111.     tableViewCell.textLabel.text = [cityList objectAtIndex:rowNo];  
  112.     // 设置UITableViewCell的详细内容  
  113.     tableViewCell.detailTextLabel.text = [NSString stringWithFormat:@"市区"];  
  114.     // 设置UITableViewCell的左边的图标  
  115.     tableViewCell.imageView.image = [UIImage imageNamed:@"1.jpg"];  
  116.     // 设置UITableViewCell附加按钮的样式  
  117.     tableViewCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  118.     //返回设置好数据的cell给UITableView对象  
  119.     return tableViewCell;  
  120. }  
  121.   
  122.   
  123. @end  

  效果图如下:

  

  


    例子二————分组展现



     ViewController.m

  

[objc] view plain copy
  1. //  
  2. //  ViewController.m  
  3. //  UITableViewSectionsApp  
  4. //  
  5. //  Created by Apple on 16/5/24.  
  6. //  Copyright © 2016年 Apple. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. NSArray* Heroes;  
  16. NSArray* Liqings;  
  17. NSArray* Kazikes;  
  18. NSArray* Rivens;  
  19.   
  20. @implementation ViewController  
  21.   
  22. - (void)viewDidLoad {  
  23.     [super viewDidLoad];  
  24.     // Do any additional setup after loading the view, typically from a nib.  
  25.       
  26.     // 分区数据  
  27.     Heroes = @[@"李青",@"卡兹克",@"瑞文"];  
  28.       
  29.     Liqings = @[@"李青1",@"李青2"];  
  30.       
  31.     Kazikes = @[@"卡兹克"];  
  32.       
  33.     Rivens = @[@"瑞文1",@"瑞文2",@"瑞文3"];  
  34.       
  35.     // 创建UITableView  
  36.     UITableView* tableView =  [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];  
  37.       
  38.     UILabel* headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0032030)];  
  39.     [headerLabel setText:@"三大英雄"];  
  40.     //设置UITable头信息  
  41.     [tableView setTableHeaderView:headerLabel];  
  42.       
  43.     UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0032030)];  
  44.     [footerLabel setText:@"等你来战"];  
  45.     //设置UITable尾部信息  
  46.     [tableView setTableFooterView:footerLabel];  
  47.       
  48.     [tableView setBackgroundColor:[UIColor cyanColor]];  
  49.       
  50.     // 添加UITableView  
  51.     [self.view addSubview:tableView];  
  52.       
  53.     // 设置数据源代理,必须实现协议UITableViewDataSource中的相关方法  
  54.     tableView.dataSource = self;  
  55.       
  56.       
  57. }  
  58.   
  59. //返回 tableView上的分区数量,本例为3  
  60. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{  
  61.     return [Heroes count];  
  62. }  
  63.   
  64. // 返回分区的title(分区是从 0 开始的)  
  65. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{  
  66.     // 根据分区的获取对应的名称  
  67.     return Heroes[section];  
  68. }  
  69.   
  70. // 返回tableView中的分区中的数据的数量  
  71. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  72.     // 根据分区,获取分区中对应要显示的数据长度  
  73.     if(section == 0){  
  74.         return [Liqings count];  
  75.     }else if(section == 1){  
  76.         return [Kazikes count];  
  77.     }else{  
  78.         return [Rivens count];  
  79.     }  
  80. }  
  81.   
  82. // 可重用标识符  
  83. static NSString* cellID = @"cellID";  
  84.   
  85. // 将提供 tableView 中显示的数据  
  86. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  87.       
  88.     // 根据cellID获取可重用的UITableViewCell对象  
  89.     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];  
  90.     if(!cell){  
  91.         //创建一个UITableViewCell对象,并绑定到cellID  
  92.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];  
  93.     }  
  94.     // 根据分区设置UITableViewCell显示的数据  
  95.     if(indexPath.section == 0){  
  96.         cell.textLabel.text = Liqings[indexPath.row];  
  97.         // 设置UITableViewCell的左边的图标  
  98.         cell.imageView.image = [UIImage imageNamed:@"l1.jpg"];  
  99.           
  100.     }else if(indexPath.section == 1){  
  101.         cell.textLabel.text = Kazikes[indexPath.row];  
  102.         cell.imageView.image = [UIImage imageNamed:@"k1.jpg"];  
  103.           
  104.     }else{  
  105.         cell.textLabel.text = Rivens[indexPath.row];  
  106.         cell.imageView.image = [UIImage imageNamed:@"r1.jpg"];  
  107.     }  
  108.     // 设置列的按钮类型  
  109.     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  110.     // 返回设置好数据的cell给UITableView对象  
  111.     return cell;  
  112. }  
  113.   
  114.   
  115.   
  116. @end  

    效果图如下:

 

     



from: iOS开发之高级视图—— UITableView(一)简单例子

http://blog.csdn.net/panjican/article/details/51493055



阅读全文
0 0
原创粉丝点击