您的位置:首页 > 产品设计 > UI/UE

UITableView的使用(熟练使用)

2016-01-10 00:33 597 查看

UITableView简单认识

什么是UITableView

在应用中能看到各式各样的
列表数据


在iOS中要想展示
列表数据
,最常用的就是用
UITableView


UITableView
继承自
UIScrollView
,因此支持垂直滚动,并且性能极佳

UITableView的样式

单组样式
UITableViewStylePlain


多组样式
UITableViewStyleGrouped


UITableView如何展示数据

UITableView需要一个
数据源(dataSource)来显示数据


UITableView会向
数据源
查询一共有多少
数据以及
每一行
显示什么数据等

没有设置数据源
的UITableView只是个
空壳


凡是遵守
UITableViewDataSource协议的OC对象
,都可以是UITableView的
数据源


UITbaleView展示数据的过程

调用数据源的下面方法得知一共有多少组数据(
若不实现此方法,默认为一组
)

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

[/code]

调用数据源的下面方法得知每一组有多少行数据

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

[/code]

调用数据源的下面方法得知每一行显示什么内容

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

[/code]

调用数据的下面方法设置头部描述

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

[/code]

调用数据的下面方法设置尾部描述

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


UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行

UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可显示一些辅助指示视图

辅助指示视图的作用是显示一个表示动作的图标,可以通过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailButton

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

还可以通过cell的accessoryView属性来自定义辅助指示视图(比如往右边放一个开关)

UITableViewCell的contentView

contentView下默认有3个子视图

其中2个是UILabel(通过UITableViewCell的textLabel和detailTextLabel属性访问)

第3个是UIImageView(通过UITableViewCell的imageView属性访问)

UITableViewCell还有一个UITableViewCellStyle属性,用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置



UITableView常用属性及方法

设置每一行cell的高度

self.tableView.rowHeight = 100;

[/code]

设置每一组头部的高度

self.tableView.sectionHeaderHeight = 80;

[/code]

设置每一组尾部的高度

self.tableView.sectionFooterHeight = 80;

[/code]

设置分割线的颜色

self.tableView.separatorColor = [UIColor clearColor] ;

[/code]

设置分割线的样式

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

[/code]

设置tableView表头view

self.tableView.tableHeaderView = [[UISwitch alloc] init];

[/code]

设置tableView表尾view

self.tableView.tableFooterView = [[UISwitch alloc] init];

[/code]

设置cell右边的指示控件

// accessoryView的优先级 > accessoryType
cell.accessoryView = [[UISwitch alloc] init];

[/code]

设置cell右边的指示样式

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

[/code]

设置cell的选中样式

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[/code]

设置cell的背景view

// backgroundView的优先级 > backgroundColor
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor blueColor];
cell.backgroundView = bg;

[/code]

设置cell的背景颜色

cell.backgroundColor = [UIColor redColor];

[/code]

设置cell选中的背景view

UIView *selectedView = [[UIView alloc] init];
selectedView.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectedView;

[/code]

选中某一行cell就会调用这个方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选中了:%zd",indexPath.row);
}

[/code]

取消选中某一行cell就会调用这个方法

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"取消选中了:%zd",indexPath.row);
}

[/code]

返回的每一组的头部标题

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [UIButton buttonWithType:UIButtonTypeContactAdd];
}

[/code]

返回的每一组的尾部标题

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

[/code]

返回的每一组的头部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}

[/code]

返回的每一组的尾部的高度

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

[/code]

返回的每一行cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 2 == 0) {
return 100;
} else {
return 50;
}
}


UITableView性能优化

每当一个cell进入可视范围内就会调用下面方法创建一个cell

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

[/code]

每当一个cell离开可视范围就会被销毁,而每当用户滚动的时候,又会从新创建一个cell,这样要造成了频繁的开辟内存和销毁内存

解决办法:

首先根据一个
标示
去缓存池找(
缓存池由TableView自动创建
)

如果在循环池找不到,就新建一个
cell
,并绑定
标示


示例代码:


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

// 用static修饰的局部变量延长生命周期,并且只会创建一次
static NSString *ID = @"car";

// 根据ID去缓存池查找是否有可重用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

// 如果找不到,就新建并绑定ID
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
NSLog(@"第%zd行的数据", indexPath.row);
}

return cell;
}

[/code]

另外一种方法

注册某个重用标示对应的cell类型(
只会执行一次
)

一般用于自定义cell

- (void)viewDidLoad {
[super viewDidLoad];

// 当View加载完毕的时候注册某个重用标示对应的cell类型
[self.tableView registerClass:[xdTableViewCell class] forCellReuseIdentifier:ID];
}


开发中必须熟练使用UITableView.以及自定义cell

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: