一、UITableView的概念

  • UITabelView继承于UIScrollView,可以滚动。
  1.   @interface UITableView : UIScrollView <NSCoding>
  • UITableView的每一条数据对应的单元格叫做Cell,是UITableViewCell的一个对象,继承于UIView。
  1.   @interface UITableViewCell : UIView <NSCoding, UIGestureRecognizerDelegate>
  • UITableView可以分区显示,每一个分区称为section,每一行称为row,编号都从0开始。
  • 系统提供了一个专门的类来整合section和row,叫做NSIndexPath。
  • UITableView的分区,叫做section;每一行,叫做row。

二、UITableView的基本使用

  • UITableView的创建
  1. // 创建对象
  2. // 初始化对象并设置样式
  3. // 系统为我们提供了两种样式(UITableViewStyleGrouped,UITableViewStylePlain)
  4. self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
  • UITableView显示的相关属性
  1.    // 设置分割线颜色(Plain样式下)
  2. self.tableView.separatorColor = [UIColor blackColor];
  3.  
  4. // 设置分割线的样式
  5. self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
  6.  
  7. // 设置行高
  8. self.tableView.rowHeight = ;
  9.  
  10. // 添加头视图,脚视图
  11. UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(, , self.frame.size.width, )];
  12. headerView.backgroundColor = [UIColor grayColor];   UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 200, self.frame.size.width,200)];    footView.backgroundColor = [UIColor grayColor];    self.tableView.tableHeaderView = headerView;    self.tableView.tableFooterView = footView;

三、UITableView显示数据

  • UITableView中有两个重要的属性:
  1.   // 显示数据相关的代理
  2.   @property (nonatomic, weak, nullable) id<UITableViewDataSource> dataSource;
  3.   // 视图操作相关的代理
  4.   @property (nonatomic, weak, nullable) id<UITableViewDelegate> delegate;
  • UITableView代理的实现代码
  1. // 设置TableView数据源代理
  2. self.rootView.tableView.dataSource = self;  // 设置TableView代理 
  3. self.rootView.tableView.delegate = self;
  • UITableView的DataSource是负责给UITableView对象提供数据的代理,遵循UITableViewDataSource协议。协议中有两个必须实现的协议方法:
  1.   // 每一个分区显示的行数(当前方法必须实现)
  2.   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  3.  
  4.   // 返回cell对象,每一行显示的内容(当前方法必须实现)
  5.   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  • UITableView的每一个单元格是UITableViewCell类的对象,继承于UIView。UITableViewCell默认提供了3个视图属性:
  1. // 标题视图
  2. cell.textLabel.text = [NSString stringWithFormat:@"朋友圈%ld", indexPath.row];
  3. // 副标题视图
  4. cell.detailTextLabel.text = @"分享";
  5. // 图片视图
  6. cell.imageView.image = [UIImage imageNamed:@"Action_Moments"];
  7. // 右侧配件(系统样式)
  8. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  9. // 右侧配件(自定义样式)
  10. cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Expression_3"]];

四、UITableViewCell的重用机制

  • 按照上面的方法,如果tableView要显示成百上千条数据时,会因创建过多的cell而导致内存过大程序不能运行。
  • 因此UITableView有一个重用池机制管理cell,目的是使用尽可能少的cell显示所有数据。
  • UITableView重用Cell的流程:

    1)当一个cell被滑出屏幕,这个cell会被系统放到相应的重用池中。

    2)当tableView需要显示一个cell,会先去重用池中尝试获取一个cell。

    3)如果重用池没有cell,就会创建一个cell。

    4)取得cell之后会重新赋值进行使用。

  1. #pragma mark UITableViewCell重用的第一种方法
  2. // 返回cell对象
  3. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  4. {
  5. static NSString *identifier = @"cell";
  6. // 1.优先从重用队列中查找标识符为cell的UITableViewCell对象
  7. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  8. if (cell == nil) {
  9. // 如果cell为空,说明重用队列中没有标识符为cell的对象,那么创建标识符为cell的UITableViewCell的对象
  10. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
  11. }
  12. // 设置cell显示的数据
  13. cell.textLabel.text = @"阿福";
  14. return cell;
  15. }
  16. #pragma mark UITableViewCell重用的第二种方法
  17. // 第一步:注册cell
  18. [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
  19. // 第二步:根据重用标识符查找cell
  20. // 如果在重用池中找到就返回,找不到就会自动创建一个并返回
  21. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

五、UITableView和数组的结合使用

  • 在延展里定义数组:
  1.   // 定义数组用来存放所有的数据
  2.   @property (nonatomic, strong) NSMutableArray *dataArray;
  • 初始化大数组
  1. // 初始化大数组
  2. self.dataArray = [NSMutableArray arrayWithArray:@[@"女神", @"傻逼", @"晒到", @"而且"]];
  • 实现协议方法
  1. // tableView每个分区要显示的个数
  2. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  3. {
  4. // 根据元素个数设置行数
  5. return self.dataArray.count;
  6. }
  1. // 返回cell
  2. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  3. {
  4. // cell重用
  5. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  6. // 根据row从数组中取值
  7. cell.textLabel.text = self.dataArray[indexPath.row];
  8. return cell;
  9. }

六、UITableView的常用协议方法

  1. // 设置分区个数
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  3. // 设置每一个cell的高度
  4. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  5. // 设置分区头的高度
  6. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
  7. // 设置分区尾的高度
  8. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
  9. // 自定义header视图
  10. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
  11. // 自定义footer视图
  12. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
  13. // 点击cell
  14. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  15. // 设置分区头标题
  16. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
  17. // 设置分区尾标题
  18. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
  19. // 设置分区索引
  20. - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;

iOS学习之UITableView的更多相关文章

  1. iOS学习之UITableView中Cell的操作

    接着iOS学习之Table View的简单使用 这篇,这里主要讲UITableView 中的Cell的操作,包括标记.移动.删除.插入. 为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http ...

  2. iOS学习之UITableView编辑

    一.UITableView编辑 UITableView编辑(删除.添加)步骤: 让TableView处于编辑状态. 协议设定:1)确定Cell是否处于编辑状态:2)设定cell的编辑样式(删除.添加) ...

  3. ios学习笔记 UITableView(纯代码) (二)

    头文件 --------------------------------------------- #import <UIKit/UIKit.h> /** UITableViewDataS ...

  4. ios学习笔记 UITableView(纯代码) (一)

    参考 “https://www.cnblogs.com/ai-developers/p/4557487.html” UITableViewCell 有一个代码重用 减少资源的浪费 参考  https: ...

  5. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  6. iOS学习笔记(4) — UITableView的 重用机制

    iOS学习笔记(4) — UITableView的 重用机制 UITableView中的cell是动态的,在使用过程中,系统会根据屏幕的高度(480)和每个cell的高度计算屏幕中需要显示的cell的 ...

  7. iOS开发系列--UITableView全面解析

    --UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...

  8. iOS学习路线图

    一.iOS学习路线图   二.iOS学习路线图--视频篇       阶 段 学完后目标 知识点 配套学习资源(笔记+源码+PPT) 密码 基础阶段 学习周期:24天       学习后目标:    ...

  9. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

随机推荐

  1. 操作系统学习笔记 对称多处理(SMP)

    SMP:一种通过复用处理器提高程序执行并行性的方式. 根据SMP,计算机系统可以分为以下四类: 单指令单数据流(SISD):一个单处理器执行一个单指令流,对保存在一个存储器中的数据进程进行操作. 单指 ...

  2. 华为OJ平台——整形数组合并

    题目描述: 将两个整型数组按照升序合并,并且过滤掉重复数组元素 输入: 输入说明,按下列顺序输入: 1 输入第一个数组的个数 2 输入第一个数组的数值 3 输入第二个数组的个数 4 输入第二个数组的数 ...

  3. noip2009提高组题解

    NOIP2009题解 T1:潜伏者 题目大意:给出一段密文和破译后的明文,一个字母对应一个密文字母,要求破译一段密文,如果有矛盾或有未出现密文无法破译输出failed,否则输出明文. 思路:纯模拟题 ...

  4. 【转】怎样提高VR渲染速度

    怎样提高VR渲染速度分析!<经验之谈>!!!VR的基本渲染方法掌握起来并不难,但是最迫切需要解决的问题是VR的出图速度问题.动则需要数小时的渲染时间真的是很难以接受,我们从三个影响速度的参 ...

  5. grub丢失的修复

    使用安装光盘进入rescure模式,经过配置后进入一个bashbash# grubgrub> root (hd0,6)grub> setup (hd0)重启即可

  6. 十一、Struts2封装请求参数的方式

    十一.Struts2封装请求参数的方式 方式一.Action 本身作为model对象,通过成员setter封装(一个名字为params的拦截器干的) 注意:表单中的名称要和动作类中的名称一致(这是必须 ...

  7. Java学生管理系统项目案例

    这是一个不错的Java学生管理系统项目案例,希望能够帮到大家的学习吧. 分代码如下 package com.student.util; import java.sql.Connection; impo ...

  8. CentOS 6.x版本升级Mysql

    首先确定一下自己的DNS  vi /etc/resolv.conf    我一般为114.114.114.114 #-----------------------------CentOS 6.x版本升 ...

  9. 线程间通信--wait和notify

    使用wait.notify方法实现线程间的通信(注意这两个方法都是object的类的方法,换句话说java为所有的对象都提供了这两个方法) 1.wait和notify必须配合synchronized关 ...

  10. SequoiaDB(巨杉数据库)(社区版)安装配置使用图解

    SequoaiDB是一款新型企业级分布式非关系型数据库,提供了基于PC服务器的大规模集群数据平台.作为全球第一家企业级文档式 NoSQL分布式数据库,为用户提供了一个高扩展性.高可用性.高性能.易维护 ...