UITableView

在iOS中显示数据列表最常用的一个控件,表格控件UITableView,它支持垂直滚动

它内置了两种样式:UITableViewStylePlain(平铺),UITableViewStyleGrouped(分组)

那么TableView是怎么展示数据的呢?

UITableView需要一个数据源(dataSource)来显示数据,UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等。没有设置数据

源的UITableView只是个空壳。凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源通常都要为UITableView设置代

理对象(delegate),以便在UITableView触发一下事件时做出相应的处理,比如选中了某一行。凡是遵守了UITableViewDelegate协议的OC对象,都

可以是UITableView的代理对象,一般会让控制器充当UITableView的dataSource和delegate。

 废话不多说 看下面 ↓

<UITableViewDataSource,UITableViewDelegate>的方法:

//有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

//第section组头部控件有多高

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

//第section组有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

//indexPath这行的cell有多高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

//indexPath这行的cell长什么样子

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

//第section组头部显示什么控件

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

//当调用reloadData的时候,会重新刷新调用数据源内所有方法,其他事情都不会做

[self reloadData]

//这个方法只有在一开始有多少条数据会算多少个高度,这个方法只会调用一次,但是每次reloadData的时候也会调用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView //右侧索引

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //行点击事件

NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //获得被选中的indexPath可以得到section,row

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; //刷新table指定行的数据

[self.tableView reloadData]; //刷新table所有行的数据

UITableView常用属性:

// 初始化表格(并设置位置,宽高,样式)。

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];

分隔线属性

tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //UITableViewCellSeparatorStyleNone;

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //取消分隔线

tableView.separatorColor = [UIColor lightGrayColor];

// 条目多选

tableView.allowsMultipleSelection = YES;

// 设置标题行高

[_tableView setSectionHeaderHeight:kHeaderHeight];

[_tableView setSectionFooterHeight:0];

// 设置表格行高

[_tableView setRowHeight:50];

//设置背景色

self.tableView.backgroundView  优先级高,如果要设置backgroundColor的时候要先把view设置为nil

self.tableView.backgroundColor = [UIColor redColor];

//在tableView的头部或者尾部添加view,footerView宽度是不用设置的

xxxView.bounds = CGRectMake(0,0,0,height);

 self.tableView.tableFooterView =xxxView;

self.tableView.tableHeaderView =xxxView;

增加tableview滚动区域

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, xx, 0);

UITableViewCell

/*
lUITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行
UITableViewCell是UIView的子类,内部有个默认的子视图:contentView。contentView是UITableViewCell所显示内容的父视图,并负责显示一些辅助指示视图。辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone
*/

//创建UITableViewCell

UITableViewCell *cell = [[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

[cell.textLabel setBackgroundColor:[UIColor clearColor]];// 清空标签背景颜色

cell.backgroundView =xx; //设置背景图片

cell.backgroundVColor =xx;//设置背景颜色

cell.selectedBackgroundView = selectedBgView; //设置选中时的背景颜色

cell.accessoryView = xxxView; //设置右边视图

[cell setAccessoryType:UITableViewCellAccessoryNone]; //设置右侧箭头

[self setSelectionStyle:UITableViewCellSelectionStyleNone]; //选中样式

cell.selectionStyle = UITableViewCellSelectionStyleBlue;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//设置cell的高度

/*

contentView下默认有3个子视图,其中的2个是UILabel,通过textLabel和detailTextLabel属性访问,第3个是UIImageView,通过imageView属性访问.

UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle

*/

#pragma mark - 重新调整UITalbleViewCell中的控件布局

- (void)layoutSubviews

{

  [super layoutSubviews];

}

cell 里面还有一个contentView

UITableViewCell表格优化

UITableViewCell对象的重用原理:

liOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决
该问题,需要重用UITableViewCell对象
重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当
UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会
用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
l还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种
UITableViewCell(如短信聊天布局),所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的
UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置
reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查
找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象

单元格优化

/*

1. 标示符统一,使用static的目的可以保证表格标示符永远只有一个

2. 首先在缓冲池中找名为"myCell"的单元格对象

3. 如果没有找到,实例化一个新的cell

*/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

  static NSString *cellIdentifier = @"myCell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

   //使用这种方法不用判断下面的cell

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

  if (cell == nil) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

     }

   return cell;

}

表格的编辑模式

删除、插入

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;  开启表格编辑状态

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

// 返回表格编辑编辑样式。不实现默认都是删除

  return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

// 根据editingStyle处理是删除还是添加操作完成删除、插入操作刷新表格

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

-(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

}

移动

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

sourceIndexPath 移动的行

destinationIndexPath 目标的行

自定义表格行UITableViewCell

storyboard方式创建:

直接拖到UITableView里面设置UITableViewCell

注意:

1.通过XIB或者Storyboard自定义单元格时,在xib和Storyboard里面需要指定单元格的可重用标示符Identifier

2.注意表格的优化中的差别

在Storyboard中两者等效

xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

xxCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

在xib文件中有差别:

第一种情况,只能在iOS 6以上使用,如果在viewDidLoad注册了nib文件,并且指定了“单元格”的可重用标示符,那么

dequeueReusableCellWithIdentifier:

dequeueReusableCellWithIdentifier:forIndexPath:

方法是等效的。如果在viewDidLoad中注册了nib文件,表格缓冲池中的管理,有系统接管!

第二种情况,是在iOS 4以上均可以使用,如果没有在viewDidLoad注册nib文件,那么,只能使用

dequeueReusableCellWithIdentifier:并且需要判断cell没有被实例化,并做相应的处理

在代码创建中差别:

用代码创建cell中的处理和nib一样,注册了cell就有系统接管并且可以用带forIndexPath的方法,没有注册就要自己去实例化cell,不能用带forIndexPath的方法

[tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];

xib方式创建:

// 注册Identifier

- (void)viewDidLoad{

[super viewDidLoad];

/**

注意:以下几句注册XIB的代码,一定要在viewDidLoad中!

注册XIB文件,获得根视图,并且转换成TableView,为tableView注册xib

Identifier名要在xib文件中定义,并且保持一致

**/

UINib *nib = [UINib nibWithNibName:@"BookCell" bundle:[NSBundle mainBundle]];

UITableView *tableView = (UITableView *)self.view;

[tableView registerNib:nib forCellReuseIdentifier:@"bookCell"];

}

// 没有注册Identifier只能使用下面方法

static NSString *CellIdentifier = @"xxxCell";

XxxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[XxxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

NSBundle *bundle = [NSBundle mainBundle];

NSArray *array = [bundle loadNibNamed:@"XxxCell" owner:nil options:nil];

cell = [array lastObject];

}

转载于:https://www.cnblogs.com/blackCat-tommy/p/3572721.html

IOS 程序猿 UITbleView 篇相关推荐

  1. 那个找不到工作的iOS程序猿

    我为什么要辞职 首先介绍一下背景,我是一枚iOS程序猿,到现在已经差不多工作3年了,今年7月底裸辞.待业之后花了半个月时间去了一趟稻城.然后差不多9月中旬左右找到新工作,开始一段新的经历. 这就是简单 ...

  2. “程序猿”面试篇(一)聊项目

    "程序猿"面试篇(一)聊项目 下面是我简历中写到的项目实战经历: 实战经历 项目经历 镇江十野物流管理系统开发/开发组成员 2019年06月 - 2020年06月 项目简介:该项目 ...

  3. 从此走上一条iOS程序猿不归路。。。

    新的城市,新的生活!前不久刚刚结束了苦逼的面试找工作之旅,期间也小有收货,如今正处年底工作闲暇之余,将前一阵子陆陆续续的总结整理了一下,本人菜鸟程序猿一只,水平有限,本文总结的知识不算深入,比较浅显, ...

  4. 程序猿投资理财篇——基金投资技巧2

           最近笔者在闲暇之余,泡了好几周基吧(基金吧)和在股票网站模拟一番,越来越起劲,对于我们程序猿来说,写代码固然重要,学会投资理财无疑让自己多了一条路,所也重要! 基金界"一姐&q ...

  5. iOS程序猿的flutter学习之路

    日常学习Flutter开发的积累 推荐一些平时自己学习Flutter开发当中接触到的优秀文章 -------------------------基础知识 ----------------------- ...

  6. swift语言前景_swift语言从天而降,作为ios程序猿,我们如果面对?

    Swift语言诞生到现在,才短短一个月多,Swift竟然挤进TIOBE排行榜的第16位,相信这发展的速度绝不是任何一个语言可与之相提并论,那Swift到底是一个怎样的语言,竟然永远如此的魅力!在如此的 ...

  7. iOS 程序猿们要知道的一些 HTTPS 的事情...

    2017年1月1日起,苹果App Store中的所有App都必须启用 App Transport Security(ATS)安全功能.App Transport Security(应用程序安全传输), ...

  8. iOS程序猿之读取手机通讯录

    读取手机通讯录 如今很多app都有读取手机联系人的需要,最近项目也遇到了类似的需求,这里整理下 从iOS开始,苹果废弃了以前复杂的AddressBookFramework,使用非常简单易用的Conta ...

  9. 教你摆脱低级程序猿 项目中cocopads的安装使用

    小农今天聊聊一款作为iOS开发者必备的第三方管理软件.希望程序猿朋友们看到小农的这篇文章后.可以真正的学会怎样灵活管理你项目中的第三方. (一)CocoaPods是什么? 首先我们来认识一下这款第三方 ...

最新文章

  1. xlsx to html c,js-xlsx使用
  2. [TCP/IP] TCP建立与终止
  3. java 判断全角_Java如何判断字符串中包含有全角,半角符号
  4. Atitit 图像处理 halcon类库的使用  范例边缘检测 attilax总结
  5. java图书管理系统个人总结_总结:JAVA小项目——图书管理系统
  6. 网络安全应急响应-流量分析技术
  7. zabbix监控nginx状态页面
  8. Queue Map 武魂融合技:MapQueue
  9. 家用数码相机选购及使用指南
  10. React Native UI渲染流程分析(Android)
  11. 插图精美的html css教程
  12. 尚硅谷大数据项目之电商数仓(4即席查询数据仓库)
  13. 大数据之Hive:正则表达式
  14. 游戏图标的设计跟其它应用的图标设计有何不同?
  15. ts(TypeScript)定义服务器返回数据类型
  16. 测试-答对5道题的人是天才,答对4道的是帅才,答对3道的是将才,答对2道的是奇才,答对1道的是人才...
  17. Photoshop图像处理算法—饱和度调整
  18. 阿里开源规则引擎QLExpress-入门实战
  19. 数字生活网络联盟(DLNA)
  20. HT1621段式液晶驱动(二)

热门文章

  1. [Vuex系列] - Module的用法(终篇)
  2. JVM解释器和编译器
  3. 语句include和require的区别是什么?
  4. Mysql ibd文件恢复指南
  5. javascript中常用数组方法详细讲解
  6. jquery 判断元素显示或隐藏
  7. Python Mixin技术介绍
  8. 在Axapta中实现trim函数
  9. influx生产部署重要配置
  10. P4068 [SDOI2016]数字配对