美文网首页
iOS--表视图(UITableViewController)的

iOS--表视图(UITableViewController)的

作者: 空白Null | 来源:发表于2016-07-07 15:09 被阅读383次

一、表视图的介绍

1、表视图,是iOS中最重要的试图,很多应用程序都会使用到,

2、表试图里面可以放很多行信息

3、表视图的两种风格

1)普通风格

UITableViewStylePlain

2)分组风格

UITableViewStyleGrouped

3)UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。

二、表视图的基本结构

1、表视图有表头、表尾、中间一连串单元格试图组成

1)设置表头

tableHeaderView

2)设置单元格试图

UITableViewCell,单元格也可以分段显示,每一段都可以通过代理设置段头和段尾

2)设置表尾

tableFooterView

3) tableView的常用属性和方法

设置表视图分割线风格

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;

设置表视图分割线颜色,默认标准灰色

@property(nonatomic,retain) UIColor *separatorColor;

设置表视图的头部视图

@property(nonatomic,retain) UIView *tableHeaderView;

设置表视图的尾部视图

@property(nonatomic,retain) UIView *tableFooterView;

设置表视图单元格的行高

@property(nonatomic) CGFloat rowHeight;

设置表视图背景

@property(nonatomic, readwrite, retain) UIView *backgroundView

刷新表视图单元格中数据

- (void)reloadData;

显示指示条

showsVerticalScrollIndicator

设置表视图section头部行高

@property(nonatomic) CGFloat sectionHeaderHeight;

设置表视图section尾部部行高

@property(nonatomic) CGFloat sectionFooterHeight

三、单元格的显示

1、单元格的位置表示

NSIndexPath:能表示当前cell是tableView的第几段第几行

2、单元格的创建

UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

cell的样式

1)UITableViewCellStyleDefault

左侧显示textLabel,imageView显示在最左边

2)UITableViewCellStyleValue1

左侧显示textLabel、右侧显示detailTextLabel,imageView显示在最左边

3)UITableViewCellStyleValue2

左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选

4)UITableViewCellStyleSubtitle

左上方显示textLabel,左下方显示detailTextLabel,imageView显示在最左边

cell的辅助图标 accessoryType

1) 不显示任何图标

UITableViewCellAccessoryNone,

2) 跳转指示图标

UITableViewCellAccessoryDisclosureIndicator

3) 内容详情图标和跳转指示图标

UITableViewCellAccessoryDetailDisclosureButton

4) 勾选图标

UITableViewCellAccessoryCheckmark

5) 内容详情图标

UITableViewCellAccessoryDetailButton

5) 自定义辅助图标

accessoryView属性

3、cell的常用属性

1)设置cell的背景试图

backgroundView

2)设置选中的cellbei的背景图片

selectedBackgroundView

3) 设置选中时的样式

selectionStyle

练习:不分组的名人录

四、数据源方法(UITableViewDatasource)

1、实例化表视图时,必须要实现他的数据源方法,以此来完成表中数据的配置,一般来说数据源方法是用来配置表中的数据

2、常用数据源方法

1)配置section中含有行数

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

2)创建单元格实例

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

3) 配置表视图section个数,默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

4)section中的头部视图的标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

5)section中的尾部视图的标题

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

/* 表视图的编辑 移动、删除等 */

6)指定单元格是否支持编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;

7)指定单元格是否支持移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;

8)用户编辑了哪一个单元格,在这里执行删除操作

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

9)实现此方法,移动单元格

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

五、代理方法(UITableViewDelegate)

1、一般是处理表视图基本样式(单元格高度)以及捕捉选中单元格事件

2、常用代理方法

1)配置行高

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

2)设置section 头部、尾部视图的高度

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

3)自定义section头部、尾部视图,注意:需要指定高度

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

4)用户单击单元格中辅助按钮时,调用该方法

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;

5)用户单击单元格,调用该方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

6)取消选中单元格时,调用该方法

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

7)设置单元格编辑样式

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

练习:分组的名字录

1、设置段头、段尾

2、自定义段头段尾

表视图常用属性和方法

属性

1、设置表视图分割线风格

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;

2、设置表视图分割线颜色,默认标准灰色

@property(nonatomic,retain) UIColor *separatorColor;

3、设置表视图的头部视图

@property(nonatomic,retain) UIView *tableHeaderView;

4、设置表视图的尾部视图

@property(nonatomic,retain) UIView *tableFooterView;

5、设置表视图单元格的行高

@property(nonatomic) CGFloat rowHeight;

6、设置表视图背景

@property(nonatomic, readwrite, retain) UIView *backgroundView

7、刷新表视图单元格中数据

- (void)reloadData;

8、设置表视图section头部行高

@property(nonatomic) CGFloat sectionHeaderHeight;

9、设置表视图section尾部部行高

@property(nonatomic) CGFloat sectionFooterHeight;

10、 刷新表视图section中数据

- (void)reloadSectionIndexTitles

11、默认为NO,不可以编辑,设置时,不存在动画效果

@property(nonatomic,getter=isEditing) BOOL editing;

12、覆盖此方法,存在动画效果

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

13、默认为YES,当表视图不在编辑时,单元格是否可以选中

@property(nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);

14、默认为NO,当表视图在编辑时,单元格是否可以选中

@property(nonatomic) BOOL allowsSelectionDuringEditing;

15、默认为NO,是否可以同时选中多个单元格,注意版本问题

@property(nonatomic) BOOL allowsMultipleSelection

17、 默认为NO,在编辑状态下时,是否可以同时选中多个单元格,注意版本问题

@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing

方法

1、指定一个cell,返回一个NSIndexPath实例,如果cell没有显示,返回nil

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

2、指定一个范围,返回一个数组,内容是NSIndexPath实例,指定rect无效,返回nil

- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;

3、指定一个NSIndexPath,返回一个cell实例,如果cell没有显示,返回为nil

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

4、根据显示的cell,返回一组cell实例的数组,如果没有显示,返回nil

- (NSArray *)visibleCells;

5、根据显示的cell,返回一组NSIndexPath实例的数组,如果没有显示,返回nil

- (NSArray *)indexPathsForVisibleRows;

6、滑动到指定的位置,可以配置动画

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

7、插入一行cell,指定一个实现动画效果

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

8、删除一行cell, 指定一个实现动画效果

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

9、刷新一个行cell,指定一个实现动画效果

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

10、移动cell的位置,指定一个实现动画效果

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath*)newIndexPath NS_AVAILABLE_IOS(5_0);

单元格的重用机制

试想如果这个表试图有几十个几百行数据的话,如果我们每次都创建一个cell将会创建太多的对象,这样会使你的软件容易卡顿,如果这个cell上面在放有高清图片的话更不可想象。

1)当表试图显示时,首先会创建几个cell显示在界面上

2)当一个cell被移出表视图时,这个cell对象并不会被释放,而是被放到一个缓存池中

3)当一个cell将要显示时,我们先要在缓存池中查看缓存池中是否有cell,如果有的话,将这个cell对象取出来复用,如果没有再重新创建

相关文章

  • iOS--表视图(UITableViewController)的

    一、表视图的介绍 1、表视图,是iOS中最重要的试图,很多应用程序都会使用到, 2、表试图里面可以放很多行信息 3...

  • IOS-- UIView中的坐标转换

    IOS-- UIView中的坐标转换 // 将像素point由point所在视图转换到目标视图view中,返回在目...

  • swift 4.2代码实现表视图UITableView

    表视图的组成 表头视图: 表视图最上边的视图,用于展示表视图的信息,也可以放搜索栏 表脚视图: 表视图最下边的视图...

  • 使用UITableViewController代替UITable

    创建一个使用UITableViewController表格视图应用 1,打开xcode,创建一个“Single V...

  • iOS--波浪视图

    效果图 正弦波纹 计算公式 y =Asin(ωx+φ)+CA表示振幅,也就是使用这个变量来调整波浪的高度ω表示周期...

  • ORACLE之视图和系统函数

    视图 视图是什么? 视图(View)是从一个或多个表(或视图)导出的表。视图与数据库中的物理表不同,视图是一个虚表...

  • 视图 约束

    视图 简单视图:单个表 复杂视图:表中包含着各种函数 连接视图:视图中牵扯到多个表 创建视图:create vie...

  • 视图--数据库2018-06-15

    视图(View)是从一个或多个表(或视图)导出的表。视图与表(有时为与视图区别,也称表为基本表--Base Tab...

  • 9_mysql视图

    ① 视图(view)是从一个或多个表中(或视图)导出的表。② 视图与表(有时为与视图区别,也称表为基本表)不同,视...

  • MySQL的视图

    MySQL的视图 创建基础表 视图的测试 视图的分类 单源表视图:视图的数据可以只取自一个基本表的部分行、列,这样...

网友评论

      本文标题:iOS--表视图(UITableViewController)的

      本文链接:https://www.haomeiwen.com/subject/dbdljttx.html