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

2023-10-26

  表视图继承自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



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS开发之高级视图—— UITableView(一)简单例子 的相关文章

随机推荐

  • 启动elasticsearch报错处理方式

    启动elasticsearch报错 bootstrap check failure 1 of 1 memory locking requested for elasticsearch process but memory is not lo
  • Flask web页面加载很慢的原因

    直接上正题吧 Flask写了个简单的页面 居然加载超过30s 排查问题发现如下 能想象加载3分钟后还是失败的心情嘛 解决方法如下 添加CDN 就是让游览器加载moment js时 不需要中介直接找到moment js文件 做法 在momen
  • Vue动态改变title的标题及图标

    1 首先安装 vue wechat title包 npm install vue wechat title save 2 引入包 设置每个页面的标题 在mian js中引入 作为全局使用 import VueWechatTitle from
  • 串口命令出现>号(大于号),无法继续执行命令,如何退出

    如果在输入无法结束 提示 gt 符号 大于号 时 可以尝试按下该组合来结束输入 ctrl c 向当前进程发送 SIGINT 信号 用于终止一个进程 ctrl z 向当前进程发送 SIGSTOP 信号 用于挂起一个进程 ctrl d 不是发送
  • React 组件的分类和Render返回值

    组件化原因 随着web的发展 许多与客户端交互的逻辑放在的客户端 用户交互 数据渲染 数据交换等 前端代码数据增多 页面逻辑复杂 难以维护 以上导致前端代码耦合度高 复用性低 开发效率底下 以上问题可以使用组件化发发解决 优点 对代码进行封
  • Java内部类

    内部类 说简单点就是一个类里面还可以定义一个类 内部类可以定义在别一个类的任意位置上 包括成员位置和局部位置 私有属性 private在本类中有效 1 内部类可以直接访问外部类中的成员 私有和非私有的都可以 2 外部类如果想要访问内部类 必
  • Linux文件创建及查看方法

    1 文件创建 vi vim 原来有文件就打开 没有就创建再打开 回车后进入命令模式 w w保存 q退出 强制 这三个可以自由组合 记住前面有冒号哦 n光标移至第n行 dd 删一行 xx 删一个 光标移至行末 G光标移至文末 查找某个字符串
  • RabbitMQ 启动报错 Failed to check/redeclare auto-delete queue(s) access to vhost '/' refused for user

    RabbitMQ 启动报错 Failed to check redeclare auto delete queue s access to vhost refused for user rabbit 今天项目在新的服务器上启动 所有的配置文
  • element-ui table 表格组件实现可拖拽效果(行、列)

    前言 最近需要实现table表格 行拖拽的功能 参照了一些优秀文章 实现了一下 参考文章 Vue进阶 幺零五 elementUI 实现表格行列拖拽 实现思路 主要是借助sortablejs 关于sortablejs我简单写了篇文章 有兴趣的
  • IDEA 最牛配置,写代码太爽了

    IDEA 最牛配置 写代码太爽了
  • qt:同一份代码在vs2022 QT VS TOOL扩展和 QtCreator下运行结果不同

    公司要求用的是QtCreator 但是谁能离得开安装了Resharper的VS呢 我就在VS下装了QT的环境 开始编写调试代码 其实是两个软件都在用的 可能是没找到方法 VS下的资源文件显示不是很方便 我就用QtCreator加资源 到后面
  • 远程RDP、远控手机、双屏控双屏,向日葵“瓜子会员”妥妥的真香

    最近儿有点 小感冒 没去公司在家歇着 居家归居家 砖还是要搬的 突然来活了也得及时的处理掉 这种时候我一般用远程桌面的方式 之前就一直用的向日葵远程控制 为啥用远程桌面呢 主要原因是家里电脑性能不如公司的工作站 而且缺少很多工作必须的专业软
  • godaddy服务器内网站转移,2021年Godaddy最新域名转出教程

    因为之前Goddady登录界面修改的原因 导致部分新手不知道Godaddy域名转出步骤 笔者特此做了一个简单的教程 供大家学习和参考 第一步 打开Godaddy官网 登录Godaddy账户 然后点击页面右侧的My Account 进入账号管
  • 实战HttpClient 接口调用以及获取token 设置请求头

    简介 HTTP 协议可能是现在 Internet 上使用得最多 最重要的协议了 越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源 虽然在 JDK 的 java net 包中已经提供了访问 HTTP 协议的基本功能 但
  • CrashImmuneDecoder类关系分析(HardwareVideoDecodeSDK)

    关于此项目github地址 https github com shyluo CrashImmuneDecoder 为了以后快速的熟悉老罗大神的视频硬解SdK 画了以下类关系图 画的不好 请见谅
  • VS2019+msys2编译ffmpeg

    最近在学习音视频相关开发技术 第一步是搭建开发环境 通过参考网上查到的资料结合实际情况 最终将ffmpeg编译通过 并支持x264 x265 fdk aac 在这里将具体的操作过程记录下来 方便以后参考 目录 1 下载VS2019社区版本
  • 【平衡小车】学习日志(八)

    任务 基于之前PID算法编写小车的可运动可平衡控制的功能代码 Control 基于之前完成的PID控制算法 修改部分编写 直立环 速度环 转向环 的控制函数 1 在Control c修改PID控制函数 直立环PD控制 直立环PD控制 参数1
  • 学机器人编程好还是学计算机编程好

    学机器人编程好还是学计算机编程好 小孩的学习一直都是家长们非常关心和重视的一件事 很多的家长在培养孩子的学习的时候 会给孩子选择一些能够有利于孩子成长的课程 就很多的家长想要孩子去学习机器人编程的课程来说 他们对于学机器人编程好还是学计算机
  • java使用POI读写Excel

    前期准备 到官网下载pol的jar包 https poi apache org 导入项目所依赖的jar包 注 这几个一个都不能少 不然会报些奇怪的错 代码 使用POI读取Excel并输出 import java io IOException
  • iOS开发之高级视图—— UITableView(一)简单例子

    表视图继承自UIScrollView 这样的继承关系使得表视图可以实现上 下滚动 UITableView需要实现的两个协议如下 UITableViewDatasource 实例化表视图时 必须采用该方法来实现数据源的配置 UITableVi