UI 高级篇:UITableView总结笔记
来源:互联网 发布:mac怎么关闭加密文件夹 编辑:程序博客网 时间:2024/05/24 05:39
笔记目录:
1 表视图一
1.1 表视图的基本概念
1.2 创建基本表格实例1
1.3 表视图的常用属性和方法
1.4 设置表视图属性实例2
1.5 表视图委托方法和数据源方法
1.6 委托方法实例3-1
1.7 委托方法实例3-2
1.8 单元格的基本概念
1.9 单元格风格和修改单元格背景实例4
1.10 单选TableView实例5
1.11 计算表视图单元格的高度实例6
1.12 索引表视图实例7
2 表视图二
2.1 表视图控制器的基本概念
2.2 单元格的常用属性
2.3 定制单元格的方式概念
2.4 第一种定制cell的方式1
2.5 第二种定制cell的方式2
2.6 第三种定制cell的方式3
2.7 编辑表视图的基本概念
2.8 课堂实例2
2.9 多选模式
2.10 课堂实例3
学习视频
无线互联iOS开发视频
详细笔记:
1 表视图一
1.1 表视图的基本概念
1) 重点掌握:常用属性和方法、数据源方法和委托方法、委托方法调用顺序、单元格的基本类型和重用
2) 表视图的两种风格:UITabelViewStylePlain、UITabelViewStyleGrouped
3) 表视图的结构
a) 表视图是由头部、尾部视图、中间一连串单元格视图组成
b) 表视图的头部是由tableHeaderView属性设置,尾部视图通过tableFooterView属性设置
c) 分组表格由一连串的section视图组成,每一个section又包含一个连续的单元格组
d) 每一个section视图也有头部(与表视图的头部不同)和尾部视图,通过委托方法设置
4) 创建一个简单地表视图
_tableView = [[UITabelView alloc]initWithFrame:CGRectMake(0,0,320,460-44) style:UITabelViewStylePlain;_tabelView.delegate = self; // 设置tableView的委托_tabelView.dataSource = self; // 设置tabelView的数据委托[self.view addSubview:_tableView];// 以下两个数据源方法必须强制实现– (NSInteger)tableView:(UITabelView *)tableViewnumberOfRowsInSection:(NSInteger)section{ return 20;} // 返回section拥有的单元格的数目,一般用数组的形式表示section中有几行[array count]– (UITabViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{satic NSString *identifier = @”myCell”;// 检查、查询是否有闲置的单元格UITabelViewCell *cell = [ tableViewdequeueReusableCellWithIdentifier:identifier];if ( cell == nil ){ cell= [[ UITabelViewCell alloc ] initWithStyle:UITabelViewCellStyleDefaultreuseIdentifier:identifier ] autorelease ];}return cell;} // 返回单元格对象
1.2 创建基本表格实例1
FontTableViewDemo
1.3 表视图的常用属性和方法
1) 常用属性
a)设置表视图分割线风格@property (nonatomic) UITableViewCellSeparatorStyleseparatorStyle;b)设置表视图分割线颜色,默认标准灰色@property (nonatomic,retain) UIColor*separatorColor;c)设置表视图的头部视图@property (nonatomic,retain) UIView*tableHeaderView;d)设置表视图的尾部视图@property (nonatomic,retain) UIView*tableFooterView;e)设置表视图单元格的行高@property (nonatomic) CGFloatrowHeight;f)设置表视图section的头部行高@property (nonatomic) CGFloatsectionHeaderHeight;g)设置表视图section的尾部行高@property (nonatomic) CGFloatsectionFooterHeight;h)设置表视图的背景@property (nonatomic,readwrite,retain)UIView *backgroundView;i)默认为NO,不可以编辑,设置时,不存在动画效果@property (nonatomic,getter=isEditing)BOOL editing;j)覆盖此方法,存在动画效果- (void)setEditing:(BOOL)editinganimated:(BOOL)animated;k)默认为YES,当表视图不存在编辑时,单元格时候可以选中@property (nonatomic) BOOL allSelectionNS——AVALIABLE_IOS(3_0);l)默认为NO,当表视图在编辑时,单元格是否可以选中@property (nonatomic) BOOLallowSelectionDuringEditing;m)默认为NO,是否可以同时选中多个单元格,注意版本问题@proerty (nonatomic) BOOLallowMultipleSelection; NS_AVALIABLE_IOS(5_0)n)默认为NO,在编辑状态下,是否可以同时选中多个单元格,注意版本问题@property (nonatomic) BOOLallowsMultipleSelectionDuringEditing; NS_AVALIABLE_IOS(5_0)
2) 常用方法(普通方法)
a)刷新表视图单元格中的数据- (void)reloadData;b)刷新表视图section中的数据- (void)reloadSectionIndexTitles;c)滑到指定行的位置,可以配置动画-(void)scrollToRowAtIndexPath:(NSIndexPath *)indexPathatScrollPosition:scrollPosition animated:(BOOL)animited;d)制定一个cell,返回NSIndexPath实例,如果cell没有显示,返回nil- (NSIndexPath*)indexPathForCell:(UITableViewCell *)cell;e)指定一个范围,返回一个数组,内容是NSIndexPath实例,指定rect无效,返回nil- (NSArray *)indexPathssForRowsInRect:(CGRect*)rect;f)指定一个NSIndexPath,返回一个cell实例,如果cell没有显示,返返回为nil- (UITabelViewCell*)cellForRowAtIndexPath:(NSIndexPath *)indexPath;g)根据显示的cell,返回一组cell实例的数组,如果没有显示,返回nil- (NSArray *)visibleCells;h)根据显示的cell,返回一组NSIndexPath实例的数组,如果没有显示,返回为nil_ (NSArray *)indexPathsForVisibleRows;
3) 常用方法(单元格:Cell的操作类))
a)插入一行cell,指定一个实现动画效果- (void)insertRowAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;b)删除一行cell,指定一个实现动画效果- (void)deleteRowAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;c)刷新一行cell,指定一个实现动画效果-(void)reloadRowsAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath*)newIndexPath NS_AVAILABLE_IOS(5_0);d)移动cell的位置,指定一个而实现动画的效果
4) NSIndexPath类介绍(索引路径)a) 概念:它表示一个路径,在嵌套数组集合中的特定的结点路径。简而言之,可以通过它获得当前表视图其中的一行。我们可以通过类方法创建一个NSIndexPath实例,来指定特定行
a)插入一行cell,指定一个实现动画效果- (void)insertRowAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;b)删除一行cell,指定一个实现动画效果- (void)deleteRowAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;c)刷新一行cell,指定一个实现动画效果-(void)reloadRowsAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath*)newIndexPath NS_AVAILABLE_IOS(5_0);d)移动cell的位置,指定一个而实现动画的效果b) NSIndexPath的常用属性和方法
// 指定特定的行和列+ (NSIndexPath *)indexPathForRow:(NSInteger *)rowinSelection:(NSInteger)section;// 指定分区@property(nonatomic,readonly)NSInteger section;// 指定行@property(nonatomic,readonly)NSInteger row;
1.4 设置表视图属性实例2
Demo
1.5 表视图委托方法和数据源方法
1) 数据源方法和委托方法
a) 表视图继承自UIScrollView,这样的继承关系使得表视图可以滚动。
b) 数据源方法:实例化表视图,必须要实现它的数据源方法,以此来完成表中数据的配置(数据源方法是用来配置表中的数据)。
c) 委托方法:一般是用来处理表视图基本样式(单元格高度)以及捕捉选中单元格事件等。
2) 创建和配置表视图的顺序
a) 创建表视图实例,初始化风格和大小
b) 设置数据源方法和委托方法
c) 开始调用数据源方法(事件循环没有结束)
d) 调用顺序
i. section的个数–(NSInteger)numberOfSelectionInTableView:(UITableView *)tableView;ii.每个section拥有的row数目– (NSInteger)tableView:(TableView*)tableView numberOfRowsInSelection:(NSInteger)section;iii.初始化Cell– (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
3) 常用的数据源方法
a)配置seciont中含有的行数- (NSInteger)tableView:(UITableView *)tableViewnumberOfRowsInSection:(NSInteger)section;b)创建单元格实例- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
@optional
c)配置表视图section的格式,默认为1-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;d)section中的头部视图的标题- (NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section;e)section中尾部视图的标题- (NSString *)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section;
/*表视图的编辑、移动、删除等*/
f)指定单元格时候支持编辑- (BOOL)tableView:(UITabelView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;g)指定单元格是否支持移动- (BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;h)用户编辑了哪一个单元格,在这里执行删除的操作- (void)tableView:(UITableView *)tabelViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath;i)实现此方法,移动单元格- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPathtoIndexPath:(NSIndexPath *)destinationIndexPath;
4) 常用的委托方法
属性相关方法
a)配置行高- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;b)设置section的头部、尾部视图的高度- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section;- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section;c)自定义section头部、尾部,注意:需要指定高度- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSIntger)section;- (UIView *)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section;单元操作相关方法
d)用户单击单元格中辅助按钮,调用该方法- (void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;e)用户单击单元格,调用该方法- (void)tableView:(UITabelView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;f)取消单元格时,调用该方法- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0)g)设置单元格编辑样式-(UITabelViewCellEditingStyle)tableView:(UITableView *)tableVieweditingStyleFowRowAtIndexPath:(NSIndexPath *)indexPath;
1.6 委托方法实例3-1
Demo1
1.7 委托方法实例3-2
Demo2
1.8 单元格的基本概念
1) 单元格的重用
a) 代码
static NSString *identifier = @””; // 静态标识符// 检测查询时是否有闲置单元格UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifier];if(cell == nil){ cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDsfaultreuseIdentifier:identifier];}// 设置cell的内容return cell;
b) 顺序
i. 定义一个静态字符串常量,制定一个标识符
ii. 检查表视图中时候存在闲置单元格,如果有取出来,如果没有则为nil
iii. 如果不存在,将会创建一个新的cell,并且指定一个标识符
2) 单元格类型
a) 第一种单元格类型(UITabelViewCellStyleDefault)if(cell == nil){ cell= [[UITabelViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier}cell.imageView.image = [UIImageimageNamed:@”t.png”];cell.textLabel.text = text;b) 第二种类型(UITabelViewCellStyleSubtitle)if (cell == nil){ cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:identifier];}cell.imageView.image = [UIImageimageNamed:@”t.png”];cell.textLabel.text = text;cell.detailTextLabel.text = text;cell.detailTextLabel.text = detailText;c) 第三种类型(UITabelViewCellSytleValue1)d) 第四种类型(UITabelViewCellSytleValue2)
3) 单元格的辅助图标类型
a)辅助图标样式1cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;b)辅助图标样式2cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;c)辅助图标样式3cell.accessoryType = UITabelViewCellAccessoryDisclosureIndicator;
1.9 单元格风格和修改单元格背景实例4
Demo4
1.10 单选TableView实例5
Demo5
1.11 计算表视图单元格的高度实例6
Demo6
1.12 索引表视图实例7
Demo7
2 表视图二
2.1 表视图控制器的基本概念
1) UITableViewController
a) UITableViewController继承UIViewController,它的创建可以极大的简化表视图的创建,默认为你实现了常用的数据源方法和代理方法(自动)。
b) 创建UITableViewController不需要我们设置数据源方法和代理方法,如果需要访问表视图,通过self.table.view
c) 如果你覆盖了loadView方法,注意确保调用父类的loadView方法,因为父类的loadView方法需要初始化UITableView
2) UITableViewController的常用属性
a) 通过这个属性,访问和设置表视图
@property(nonatomic,retain)UITableView *tableView;
b) 默认为YES,当视图出现时,是否取消选中状态
@property(nonatomic) BOOLclearSelectionOnViewWillAppear;
2.2 单元格的常用属性
1) 默认为nil,如你需要它将会被创建
@property(nonatomic,readonly,retain)UIImageView *imageView NS_AVILABLE_IOS(3_0);
2) 默认为nil,如果你需要它将会被创建
@property(nonatomic,readonly,retain)UILabel *textLabel NS_AVAILABLE_IOS(3_0);
3) 默认为nil,如果你需要它将会被创建,注意需要选择表视图的风格
@property(nonatomic,readonly,retain)UILabel *detailTextLabel NS_AVAILABLE_IOS(3_0);
4) 添加自定义视图,需要添加在contentView中,如果你直接添加在cell中,那么当编辑模式时,它的位置不会发生改变,因此我们添加自定义视图时,需要添加到contentView中
@property(nonatomic,readonly,retain)UIView *contentView;
5) 通过这个属性定制单元格背景
@property(nonatomic,retain) UIView*backgroundView;
6) 通过这个属性定制单元格的选中背景
@property(nonatomic,retain) UIView *selectedBackgroundView;
7) 多选时选中的背景视图
@property(nonatomic,retain) UIView*mutipleSelectionBackgroundView; NS_AVAILABLE_IOS(5_0);
8) 获取单元格标识符
@property(nonatomic,readonly,copy)NSString *reuseIdentifier;
9) 设置单元格选中风格
@property(nonatomic)UITableViewCellSelectionStyle sectionStyle;
10) 获取单元格编辑风格
@property(nonatomic,readonly)UITableViewCellEditingStyle editingStyle;
11) 设置单元格辅助图标类型
@property(nonatomic,retain) UIView*accessoryView;
12) 自定义辅助图标
@property(nonatomic,retain) UIView*accessoryView;
13) 自定义编辑图标
@property(nonatomic,retain) UIView*editingAccessoryView;
2.3 定制单元格的方式概念
1) 定制单元格的几种方式
a) 通过UITableViewCell固定格式设置,其属性是imageView,textLabel、detailLabel,但是他们的样式固定,且通常来说不易改变他们的位置,不够灵活
b) 通过UITableViewCell的contenView添加子视图
c) 使用xib自定义子视图,布局十分方便,开发较为迅速
d) 子类化UITabelViewCell,更加面向对象
2) 固有样式位置
a)改变系统样式位置- (void)layoutSubViews { [superlayoutSubviews]; self.textLabel.frame= CGRectMake(10,5,200,20); self.detailLabel.frame= CGRectMake(10,30,100,10); self.imageView.frame= CGRectMake(260,30,50,10);}
3) 定制单元格详解
a)向contentView中添加子视图if(cell == nil){ cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier]; UILabel *titleLab = [[UILabelalloc]initWithFrame:CGRectMake(10,5,200,20)]; titleLab.tag = 100;titleLab.font = [UIFontboldSystemFontOfSize:14.0f];titleLab.backgroundColor = [UIColorclearColor];[cell.contentViewaddSubView:titleLab];//添加其他子视图}UILabel *titleLab = (UILabel*)[cell.contentView viewWithTag:100]; UILabel *titleLab = (UILabel*)[cell.contentView viewWithTag:100]; titleLab.text = @”label的内容”; b)使用xib定义单元格if (cell == nil){ NSBundle*bundle = [NSBundle mainBundle]; //加载xib NSArray*array = [bundle loadNibNamed:@”newsCell” owner:self options:nil]; cell= [array objectAtIndex:0];}UILabel *titleLab = (UILabel*)[cell.contentView viewWithTag:100];title.Lab = (UILabel*)[cell.contentView viewWithTag:100];titleLab.text = @”label内容”; c)子类化定制- (void)_initViews{ _titleLab= [[UILabel alloc]initWithFrame:CGRectZero]; _titleLab.font= [UIFont boldSystemFontOfSize:14.0f]; _titleLab.backgroundColor= [UIColor clearColor]; [self.contentViewaddSubview:_titleLab]; //.....初始化其他UI控件} - (void)setNews:(News *)news{ _titleLab.text= news.title; _commentLab.text= [NSString stringWithFormat:@”%条评论”,news.commentCount]; _timeLab.text= [NSString stringWithFormat:@”%d小时前”,news.timeVal];} - (void)layoutSubviews{ [superlayoutSubviews]; _titleLab.frame= CGRectMake(10,5,200,20); _commentLab.frame= CGRectMake(10,30,100,10); _timeLab.frame= CGRectMake(260,30,50,10);}
2.4 第一种定制cell的方式1
Demo1
2.5 第二种定制cell的方式2
Demo2
2.6 第三种定制cell的方式3
Demo3
2.7 编辑表视图的基本概念
1) 表视图的编辑模式有三种:添加、删除、移动
2) UITableView的编辑模式流程
3) UITableView编辑模式的核心代码
// 新增、删除按钮事件- (void)tableView:(UITabelView *)tableViewcommitEditingStyle:(UITabelViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath{ // 删除 if(editingStyle== UITabelViewCellEditingStyleDelete){ [_dataremoveObjectAtIndex:indexPath.row]; [tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];}// 新增else if(editingStyle ==UITabelViewCellEditingStyleInsert){ NSString*obj = [NSString stringWithFormat@”我是新添加的”; [_datainsertObject:obj atIndex:indexPath.row]; [tableViewinsertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];}}
4) UITableView编辑模式数据源方法
a)定义可编辑的单元格- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;b)定义编辑模式下,按钮的现实样式,有新增、删除- (UITableViewCellEditingStyle)tabelView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;c)新增、删除按钮- (void)tableView:(UITableView *)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath;d)实现此方法,单元格即可移动- (void)tableView:(UITabelView*)tableView moveRowAtIndexPath:(NSIndexPath *)indexPathtoIndexPath:(NSIndexPath *)toIndexPath;e)指定可移动的单元格-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
2.8 课堂实例2
Demo
2.9 多选模式
1) 表视图的单选模式(IOS_5.0)
a)在编辑模式下支持多选self.tableView.allowMultipleSelectionDuringEditing= YES;b)选中后cell索引集合NSArray *indexPaths =self.tableView.indexPathForSelectedRows;
2.10 课堂实例3
Demo
- UI 高级篇:UITableView总结笔记
- UI基础-UITableView 高级
- UI基础之------UITableView高级
- iOS笔记UI--UITableView
- J2ME高级UI总结
- UI高级---->多线程总结
- UI 常用方法总结之--- UITableView
- UI高级-------- block 的总结
- NC高级UI工厂笔记
- NC高级UI工厂笔记
- iOS开发UI篇 -- UITableView
- UITableView 高级
- UITableView高级
- UI---UITableView
- UI - UITableView
- UI控件笔记(十一):UI之UITableView\自定义UITableView\分组
- ios-UI高级 多线程部分内容总结
- UI 常用方法总结之--- UITableView (不断更新中)
- 人脸识别图像库
- 华为C/C++笔试题
- 【浅析】ActiveMQ传输文件的几种方式原理与优劣
- mesa
- java properties文件的读写
- UI 高级篇:UITableView总结笔记
- c#+EmguCv 图像灰度化
- 解决Android解析图片的OOM问题!!!
- POJ 3498 March of the Penguins
- 乘积能被10整除的概率
- 记一下jar class path 需要注意的问题
- 恶搞突然有人同一天饿一天
- JMS五种消息的发送/接收的例子
- PHP如何解决网站大流量与高并发的问题