IOS之UITableView详解(2)

  1 UITableView
  2 UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped
  3
  4 <UITableViewDataSource,UITableViewDelegate>里的方法:
  5 tableView处理步骤
  6 #pragma mark 1.有多少组
  7 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  8 #pragma mark 2.第section组头部控件有多高
  9 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
 10 #pragma mark 3.第section组有多少行
 11 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 12 #pragma mark 4.indexPath这行的cell有多高
 13 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 14 #pragma mark 5.indexPath这行的cell长什么样子
 15 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 16 #pragma mark 6.第section组头部显示什么控件
 17 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
 18
 19
 20 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 21
 22 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 23
 24 //每当有一个cell进入视野屏幕就会调用,所以在这个方法内部就需要优化。
 25 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 26 if(cell==nil){
 27      //在这里面做创建的工作。循环优化。防止刷新cell进入屏幕的时候重复的创建
 28 }
 29 }
 30
 31 //当调用reloadData的时候,会重新刷新调用数据源内所有方法,其他事情都不会做呀
 32  [self reloadData]
 33
 34  //这个方法只有在一开始有多少条数据才会算多少个高度,这个方法只会调用一次,但是每次reloadData的时候也会调用
 35  //而且会一次性算出所有cell的高度,比如有100条数据,一次性调用100次
 36 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 37
 38
 39
 40
 41 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView //右侧索引
 42
 43     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //行点击事件
 44
 45 NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //获得被选中的indexPath可以得到section,row
 46
 47 [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; //刷新table指定行的数据
 48
 49    [self.tableView reloadData]; //刷新table所有行的数据
 50
 51
 52 UITableView常用属性:
 53     UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain]; // 初始化表格
 54     分隔线属性
 55 tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //UITableViewCellSeparatorStyleNone;
 56 [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //取消分隔线
 57     tableView.separatorColor = [UIColor lightGrayColor];
 58
 59     条目多选
 60 tableView.allowsMultipleSelection = YES;
 61
 62     // 设置标题行高
 63 [_tableView setSectionHeaderHeight:kHeaderHeight];
 64     [_tableView setSectionFooterHeight:0];
 65
 66     // 设置表格行高
 67  [_tableView setRowHeight:50];
 68
 69 //设置背景色
 70 self.tableView.backgroundView  优先级高,如果要设置backgroundColor的时候要先把view设置为nil
 71 self.tableView.backgroundColor
 72
 73 //在tableView的头部或者尾部添加view,footerView宽度是不用设置的
 74        xxxView.bounds = CGRectMake(0,0,0,height);
 75     self.tableView.tableFooterView =xxxView;
 76        self.tableView.tableHeaderView =xxxView;
 77
 78 UIButton *bt = (UIButton*)[self.contentView viewWithTag:i+100];
 79
 80 增加tableview滚动区域
 81 self.tableView.contentInset = UIEdgeInsetsMake(0, 0, xx, 0);
 82 UITableViewCell
 83 //创建UITableViewCell
 84 UITableViewCell *cell = [[UITableViewCell alloc]
 85 initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
 86
 87     [cell.textLabel setBackgroundColor:[UIColor clearColor]];// 清空标签背景颜色
 88
 89         cell.backgroundView =xx; //设置背景图片
 90         cell.backgroundVColor =xx;
 91         cell.selectedBackgroundView = selectedBgView; //设置选中时的背景颜色
 92
 93
 94    cell.accessoryView = xxxView; //设置右边视图
 95    [cell setAccessoryType:UITableViewCellAccessoryNone]; //设置右侧箭头
 96
 97 [self setSelectionStyle:UITableViewCellSelectionStyleNone]; //选中样式
 98 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
 99
100 //设置cell的高度
101 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
102
103 contentView下默认有3个子视图,其中的2个是UILabel,通过textLabel和detailTextLabel属性访问,第3个是UIImageView,通过imageView属性访问.
104   UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle
105
106 #pragma mark - 重新调整UITalbleViewCell中的控件布局
107 - (void)layoutSubviews{
108 [super layoutSubviews];
109 …
110 }
111 cell 里面还有一个contentView
112 UITableViewCell表格优化
113 UITableViewCell对象的重用原理:
114 重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
115 还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell(如短信聊天布局),所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,时可能会得到错误类型的UITableViewCell那么UITableView在重用UITableViewCell。解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
116
117 /**
118 单元格优化
119  1. 标示符统一,使用static的目的可以保证表格标示符永远只有一个
120  2. 首先在缓冲池中找名为"myCell"的单元格对象
121  3. 如果没有找到,实例化一个新的cell
122  **/
123 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
124     static NSString *cellIdentifier = @"myCell";
125 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
126 //使用这种方法不用判断下面的cell
127     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
128     if (cell == nil) {
129         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
130     }
131 return cell;
132 }
133
134 表格的编辑模式
135 删除、插入
136 - (void)setEditing:(BOOL)editing animated:(BOOL)animated;  开启表格编辑状态
137
138 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
139     返回表格编辑编辑样式。不实现默认都是删除
140 return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert
141 }
142
143 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
144       //根据editingStyle处理是删除还是添加操作
145       完成删除、插入操作刷新表格
146 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
147
148 -(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
149 }
150
151 移动
152 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
153 sourceIndexPath 移动的行
154 destinationIndexPath 目标的行
155
156 自定义表格行UITableViewCell
157 storyboard方式创建:
158 直接拖到UITableView里面设置UITableViewCell
159 注意:
160 1.通过XIB或者Storyboard自定义单元格时,在xib和Storyboard里面需要指定单元格的可重用标示符Identifier
161
162 2.注意表格的优化中的差别
163 在Storyboard中两者等效
164 xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
165 xxCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
166
167 在xib文件中有差别:
168 第一种情况,只能在iOS 6以上使用,如果在viewDidLoad注册了nib文件,并且指定了“单元格”的可重用标示符,那么
169      dequeueReusableCellWithIdentifier:
170      dequeueReusableCellWithIdentifier:forIndexPath:
171      方法是等效的。如果在viewDidLoad中注册了nib文件,表格缓冲池中的管理,有系统接管!
172
173 第二种情况,是在iOS 4以上均可以使用,如果没有在viewDidLoad注册nib文件,那么,只能使用
174      dequeueReusableCellWithIdentifier:并且需要判断cell没有被实例化,并做相应的处理
175
176
177 在代码创建中差别:
178 用代码创建cell中的处理和nib一样,注册了cell就有系统接管并且可以用带forIndexPath的方法,没有注册就要自己去实例化cell,不能用带forIndexPath的方法
179 [tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];
180
181 xib方式创建:
182 //注册Identifier
183 - (void)viewDidLoad{
184     [super viewDidLoad];
185     /**
186      注意:以下几句注册XIB的代码,一定要在viewDidLoad中!
187      注册XIB文件,获得根视图,并且转换成TableView,为tableView注册xib
188      Identifier名要在xib文件中定义,并且保持一致
189      **/
190     UINib *nib = [UINib nibWithNibName:@"BookCell" bundle:[NSBundle mainBundle]];
191     UITableView *tableView = (UITableView *)self.view;
192     [tableView registerNib:nib forCellReuseIdentifier:@"bookCell"];
193 }
194
195 //没有注册Identifier只能使用下面方法
196 static NSString *CellIdentifier = @"bookCell";
197 BookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
198 if (cell == nil) {
199         cell = [[BookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
200         NSBundle *bundle = [NSBundle mainBundle];
201         NSArray *array = [bundle loadNibNamed:@"BookCell" owner:nil options:nil];
202         cell = [array lastObject];
203     }
204
205
206 代码方式创建:
207 1.    建立UITableViewCell的类,继承UITableViewCell
208 2.    往cell里面加入view的时候注意点:
209 //新建的组件放入contentView中
210 [self.contentView addSubview:xxView];
211
212 //设置图片拉伸属性stretch
213 UIImage *normalImage = [UIImage imageNamed:@"xx.png"];
214 normalImage = [normalImage stretchableImageWithLeftCapWidth:
215 normalImage.size.width / 2 topCapHeight:normalImage.size.height / 2];
216
217 //在tableView里面viewDiDLoad里面要注册cell类
218 [tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];
219
220 自定义表格中Header
221 //自定义表格在这个方法中定义
222 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
223 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
时间: 2024-12-24 08:46:36

IOS之UITableView详解(2)的相关文章

iOS开发- UICollectionView详解+实例

iOS开发- UICollectionView详解+实例 本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类. 使用UICollectionView 必须实现UICol

iOS开发者证书-详解

iOS开发者证书-详解/生成/使用 本文假设你已经有一些基本的Xcode开发经验, 并注册了iOS开发者账号. 相关基础 加密算法 现代密码学中, 主要有两种加密算法: 对称密钥加密 和 公开密钥加密. 对称密钥加密 对称密钥加密(Symmetric-key algorithm)又称为对称加密, 私钥加密, 共享密钥加密. 这类算法在加密和解密时使用相同的密钥. 例如: 最常见的应用场景 - 系统登陆. 要成功登陆系统, 你必须输入正确的密码, 这密码是唯一的, 是与创建时一样的. 同样 的,

UITableView 详解 ()

(原本取至D了个L微信公众号) UITableView 详解 一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTable setDelegate:self]; [DataTable setDataSource:self]; [self.view addSubview:DataTable]; [DataTable release]; 二.UITable

转:iOS中socket详解

一.网络各个协议:TCP/IP.SOCKET.HTTP等 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被称作媒体层,是网络工程师所研究的对象: 传输层.会话层.表示层和应用层则被称作主机层,是用户所面向和关心的内容. http协议   对应于应用层 tcp协议    对应于传输层 ip协议     对应于网络层 三者本质上没有可比性.  何况HTTP协议是基于TCP连接的. TCP/IP是传输层协议,主要解决数据如何在网络

iOS UIView动画详解(Objective-C)

我在之前的一篇博客中<iOS UIView动画详解(Swift)>讲解了使用Swift来实现UIView类下面提供的多种动画效果,如位置动画.旋转动画.缩放动画.颜色动画.透明度动画等等.为了这个题目的完整性,今天我使用Objective-C来完全重写以上的所有的动画.项目案例已经上传至:https://github.com/chenyufeng1991/iOS-UIView-Animation  中的Animation-OC文件夹下,另一个目录下则是Swift实现的动画. (1)位置动画 P

iOS开发:详解Objective-C runTime

Objective-C总Runtime的那点事儿(一)消息机制 最近在找工作,Objective-C中的Runtime是经常被问到的一个问题,几乎是面试大公司必问的一个问题.当然还有一些其他问题也几乎必问,例如:RunLoop,Block,内存管理等.其他的问题如果有机会我会在其他文章中介绍. 本篇文章主要介绍RunTime. RunTime简称运行时.就是系统在运行的时候的一些机制,其中最主要的是消息机制.对于C语言,函数的调用在编译的时候会决定调用哪个函数( C语言的函数调用请看这里 ).编

iOS开发 - UIActivityViewController详解

昨天在做微信分享的时候, 用到了这个东西.趁热写点东西记录下. UIActivityViewController类是一个标准的view controller,通个使用这个controller,你的应用程序就可以提供各种服务. 系统提供了一些通用的标准服务,例如拷贝内容至粘贴板.发布一个公告至社交网.通过email或者SMS发送内容. 应用程序同样可以自定义服务.(我的微信分享就属于自定义服务, 之后将会写一篇教程介绍) 你的应用程序负责配置.展现和解雇这个view controller. vie

转载]IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本 )

原文地址:IOS LBS功能详解[0](获取经纬度)[1](获取当前地理位置文本作者:佐佐木小次郎 因为最近项目上要用有关LBS的功能.于是我便做一下预研. 一般说来LBS功能一般分为两块:一块是地理定位,就是获取当前精度.纬度和地理位置的功能,这一部分功能主要用到CoreLocation.Frameworks.一部分就是显示地图信息.丰富地图内容等,这一部分主要用到MapKit.Frameworks.以上这几个功能的测试最好都要在真机上进行.模拟器上定位一般会在Apple的加州总部. 首先介绍

IOS开发 Blocks详解(转)

IOS开发 Blocks详解(转) (2013-10-14 16:41:54) 从Mac OS X 10.6以及iOS 4开始,苹果在GCC和Clang编译器中为C语言引入了一个新扩展:Blocks,使得程序员可以在C.Objective-C.C++和Objective-C中使用闭包.Blocks有点像函数,但是它可以在其它函数或方法中进行声明和定义,同时它还是匿名的(匿名函数),并可以捕获其所在作用域中的变量(闭包特性). Blocks的语法 Blocks和C语言中的函数指针有点类似,如果你了