UITableview

1、表视图的基本概念

·UITableView的基本概念
·在iOS开发中,表视图的应用十分广泛与普遍。因此,熟练使用表视图以及学习其原 理显得至关重要。
·我们可以选择创建表视图也可以直接选择创建表视图控制器 
·UITableView基本样式



·UITableView的风格
·表视图存在两种显示风格,UITableViewStylePlain、UITableViewStyleGrouped



·表视图的结构
·表视图由头部、尾部视图,中间由一连串单元格视图组成 
·表视图的头部视图由tableHeaderView属性设置,尾部视图通过tableFooterView属性设置
·分组表格由一连串section(默认是1)视图组成,每一个section又包含一个连续 的单元格
·每一个section视图又有头部视图和尾部视图,通过委托方法设置

·创建一个简单的表视图

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. _tableView=[[UITableView alloc] initWithFrame:CGRectMake(00320460-44) style:UITableViewStylePlain];  
  2. _tableView.delegate = self// 设置tableView的委托  
  3. _tableView.dataSource = self// 设置tableView的数据委托  
  4. [self.view addSubview:_tableView];  
  5. // 以下两个数据源方法必须强制实现  
  6. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {  
  7. return 20;  
  8. // 返回每个section中的单元格数,⼀一般以数组的形式表示section中有几行[array count]  
  9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  
  10. (NSIndexPath *)indexPath {  
  11. static NSString *identifier = @"myCell";  
  12. // 检测、查询是否有闲置的单元格  
  13. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) {  
  14.           cell = [[[UITableViewCell alloc]  
  15.                  initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]  
  16. autorelease];  
  17.     }  
  18. return cell;  
  19. // 返回每行的单元格对象  

2、表视图常用属性和方法

·常用属性

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 设置表视图分割线风格  
  2. @property(nonatomic) UITableViewCellSeparatorStyle separatorStyle; // 设置表视图分割线颜色,默认标准灰色  
  3. @property(nonatomic,retainUIColor *separatorColor;  
  4. // 设置表视图的头部视图  
  5. @property(nonatomic,retainUIView *tableHeaderView;  
  6. // 设置表视图的尾部视图  
  7. @property(nonatomic,retainUIView *tableFooterView;  
  8. // 设置表视图单元格的行高  
  9. @property(nonatomic) CGFloat rowHeight;  
  10. // 设置表视图section头部行高  
  11. @property(nonatomic) CGFloat sectionHeaderHeight;  
  12. // 设置表视图section头部行高  
  13. @property(nonatomic) CGFloat sectionFooterHeight;  
  14. // 设置表视图背景  
  15. @property(nonatomicreadwriteretainUIView *backgroundView  
  16. // 刷新表视图单元格中数据  
  17. - (void)reloadData;  
  18. // 刷新表视图section中数据  
  19. - (void)reloadSectionIndexTitles  
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 默认为NO,不可以编辑,设置时,不存在动画效果   
  2. @property(nonatomic,getter=isEditing) BOOL editing;  
  3. // 覆盖此方法,存在动画效果  
  4. - (void)setEditing:(BOOL)editing animated:(BOOL)animated;  
  5. // 默认为YES,当表视图不在编辑时,单元格是否可以选中  
  6. @property(nonatomicBOOL allowsSelection NS_AVAILABLE_IOS(3_0);  
  7. // 默认为NO,当表视图在编辑时,单元格是否可以选中  
  8. @property(nonatomicBOOL allowsSelectionDuringEditing;  
  9. // 默认为NO,是否可以同时选中多个单元格,注意版本问题  
  10. @property(nonatomicBOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0);   
  11. // 默认为NO,在编辑状态下时,是否可以同时选中多个单元格,注意版本问题 @property(nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0);  

·常用方法

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  指定⼀一个cell,返回⼀一个NSIndexPath实例,如果cell没有显示,返回nil  
  2. - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;  
  3. // 指定⼀一个范围,返回⼀一个数组,内容是NSIndexPath实例,指定rect无效,返回nil  
  4. - (NSArray *)indexPathsForRowsInRect:(CGRect)rect;  
  5. // 指定⼀一个NSIndexPath,返回⼀一个cell实例,如果cell没有显示,返回为nil  
  6. - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;  
  7. // 根据显示的cell,返回⼀一组cell实例的数组,如果没有显示,返回nil  
  8. - (NSArray *)visibleCells;  
  9. // 根据显示的cell,返回⼀一组NSIndexPath实例的数组,如果没有显示,返回nil  
  10. - (NSArray *)indexPathsForVisibleRows;  
  11. // 滑动到指定的位置,可以配置动画  
  12. - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;  
  13. //  插入⼀一行cell,指定⼀一个实现动画效果  
  14. - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;  
  15. // 删除⼀一行cell, 指定⼀一个实现动画效果  
  16. - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;  
  17. // 刷新⼀一个行cell,指定⼀一个实现动画效果  
  18. - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);  
  19. // 移动cell的位置,指定⼀一个实现动画效果  
  20. - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0);  

索引路径
·NSIndexPath类
·它表示是一个路径,在嵌套数组 集合中的特定的结点路径。简而言 之,可以通过它获得到当前表视图 其中的一行
·我们可以通过类方法创建一个 NSIndexPath实例,来指定特定行


·常用属性和方法

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  指定特定的行和列  
  2. + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section;  
  3. @property(nonatomic,readonly) NSInteger section; // 指定分区   
  4. @property(nonatomic,readonly) NSInteger row; // 指定行  

3、数据源方法和委托方法

·数据源方法和委托方法
·表视图的继承自UIScrollView,这样的继承关系使得表视图可以实现上、下滚动, 它的父类,我们将在后面的课程中再次提及
·数据源方法(UITableViewDatasource): 实例化表视图时,必须要实现它的数据 源方法,以此来完成表中数据的配置(一般来说数据源方法是用来配置表中的数据)
·委托方法(UITableViewDelegate): 表视图的委托方法(代理方法),一般是处 理表视图基本样式(单元格的高度)以及捕捉选中单元格选中事件等


表示视图调用顺序—委托方法

·创建和配置表视图的顺序
·创建表视图实例,初始化风 格和大小
·设置数据源方法和委托方法 
·开始调用数据源方法,(注意事件循环没有结束)

·调用顺序如下图所示:



[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 1,默认为1   
  2. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 2  
  3. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 3  

常用数据源方法和委托方法

·常用数据源方法

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  配置section中含有行数  
  2. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section;  
  3. // 创建单元格实例  
  4. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;  
  5. @optional  
  6. // 配置表视图section个数,默认为1  
  7. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  
  8. // section中的头部视图的标题  
  9. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section;  
  10. // section中的尾部视图的标题  
  11. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection: (NSInteger)section;  

数据源方法

·常用数据源方法

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /* 表视图的编辑 移动、删除等 */  
  2. // 指定单元格是否支持编辑  
  3. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;  
  4. // 指定单元格是否支持移动  
  5. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;  
  6. // 用户编辑了哪⼀一个单元格,在这里执行删除操作  
  7. - (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;  
  8. // 实现此方法,移动单元格  
  9. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;  

常用委托方法

·常用委托方法

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 配置行高  
  2. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath;  
  3. // 设置section 头部、尾部视图的高度  
  4. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection: (NSInteger)section;  
  5. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection: (NSInteger)section;  
  6. // 自定义section头部、尾部视图,注意:需要指定高度  
  7. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section;  
  8. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection: (NSInteger)section;  
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 用户单击单元格中辅助按钮时,调用该方法  
  2. - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;  
  3. // 用户单击单元格,调用该方法  
  4. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath;  
  5. // 取消单元格时,调用该方法  
  6. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath: (NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);  
  7. // 设置单元格编辑样式  
  8. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;  

4、单元格的基本用法

·单元格的重用
·考虑这样的一种问题,假设表视图中有上百个联系人(甚至更多),那么我们需要 创建成百乃至上千个单元格对象吗?答案是否定的!

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static NSString *identifier = @"myCell"// 静态标识符  
  2. // 检测查询是否有闲置的单元格  
  3. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  4.       if (cell == nil) {  
  5.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];   
  6. }  
  7. // 设置cell的内容  
  8. return cell;  

节省内存方式就是重用单元格
如若表视图显示10个单元格,创建11个就能满足需求
 ·首先,定义一个静态字符串常量,指定一个标识符
 ·其次,检查表视图中是否存在闲置单元格,如果有取出来,如果没有则为nil
 ·如若不存在,将会创建一个新的cell,并且指定一个标识符 


单元格类型

·第一种单元格类型
·UITableViewCellStyleDefault

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (cell == nil) {  
  2.    cell = [[[UITableViewCell alloc initWithStyle: UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];  
  3. }  
  4. cell.imageView.image = [UIImage imageNamed:@"t.png"];  
  5. cell.textLabel.text = text;  



·第二种类型
·UITableViewCellStyleSubtitle

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. if (cell == nil) {  
  2.    cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:identifier]  autorelease];  
  3. }  
  4. cell.imageView.image = [UIImage imageNamed:@"t.png"];  
  5. cell.textLabel.text = text;  
  6. cell.detailTextLabel.text =detailText;  





单元格的辅助图标类型

·设置辅助图标(默认为None)

·辅助图标样式1

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


·辅助图标样式2

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;



·辅助图标样式3

cell.accessoryType = UITableViewCellAccessoryCheckmark;



·创建单选表视图
·核心代码

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. NSIndexPath *lastIndexPath =[NSIndexPath indexPathForRow:isSelected inSection:0];  
  2. UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndexPath];  
  3. lastCell.accessoryType = UITableViewCellAccessoryNone;  
  4. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];  
  5. cell.accessoryType = UITableViewCellAccessoryCheckmark;  
  6. isSelected = indexPath.row;  

·单元格高度自适应

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. - (float)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {  
  2.     NSString *text = [_data objectAtIndex:indexPath.row];  
  3.     CGSize size = [text sizeWithFont: [UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300,1000)];  
  4.     return size.height+20;  
  5. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值