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

IOS UitableView 介绍

2015-07-13 14:19 387 查看
在众多移动应⽤用中,能看到各式各样的表格数据 ,

• 在iOS中,要实现表格数据展⽰示,最常⽤用的做法就是使⽤用UITableView

• UITableView继承⾃自UIScrollView,因此⽀支持垂直滚动,⽽而且性能极佳

UITableView的两种样式,一种是只有一组别,还有一种是有多个数别如下图:



如何展⽰示数据 ?

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

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

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

tableView展⽰示数据的过程

1. 调⽤用数据源的下⾯面⽅方法得知⼀一共有多少组数据

- (NSInteger)numberOfSectionsInTableView:(UITableView

*)tableView;

2. 调⽤用数据源的下⾯面⽅方法得知每⼀一组有多少⾏行数据

- (NSInteger)tableView:(UITableView *)tableView

numberOfRowsInSection:(NSInteger)section;

3. 调⽤用数据源的下⾯面⽅方法得知每⼀一⾏行显⽰示什么内容

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


当然,我们还要实现协议,不然,这些代理方法也无法使用。

property (nonatomic, assign) id <UITableViewDataSource> dataSource; UITableView

通过这3个代理方法,就可以把想要显示在页面的数据,绑定到UITableView上。

二 Cell简介

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

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

辅助指⽰示视图的作⽤用是显⽰示⼀一个表⽰示动作的图标,可以通过设置UITableViewCell的 accessoryType来显⽰示,

默认是UITableViewCellAccessoryNone(不显⽰示辅助指⽰示视图), 其他值如下:

● UITableViewCellAccessoryDisclosureIndicator

● UITableViewCellAccessoryDetailDisclosureButton

● UITableViewCellAccessoryCheckmark

• 还可以通过cell的accessoryView属性来⾃自定义辅助指⽰示视图

UITableViewCell的contentView

• contentView下默认有3个⼦子视图

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

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

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



Cell的重⽤用原理

• iOS设备的内存有限,如果⽤用UITableView显⽰示成千上万条数据,就需要成千上万 个UITableViewCell对象的话,那将会耗尽iOS设备的内存。

要解决该问题,需要重 ⽤用UITableViewCell对象

• 重⽤用原理:当滚动列表时,部分UITableViewCell会移出窗⼜⼝口,UITableView会将窗⼜⼝口外 的UITableViewCell放⼊入⼀一个对象池中,等待重⽤用。

当UITableView要求dataSource返回 UITableViewCell时,dataSource会先查看这个对象池,

如果池中有未使⽤用的 UITableViewCell,dataSource会⽤用新的数据配置这个UITableViewCell,

然后返回给 UITableView,重新显⽰示到窗⼜⼝口中,从⽽而避免创建新对象

• 还有⼀一个⾮非常重要的问题:有时候需要⾃自定义UITableViewCell(⽤用⼀一个⼦子类继 承UITableViewCell),

⽽而且每⼀一⾏行⽤用的不⼀一定是同⼀一种UITableViewCell,所以⼀一 个UITableView可能拥有不同类型的UITableViewCell,

对象池中也会有很多不同类型的 UITableViewCell,那么UITableView在重⽤用UITableViewCell时可能会得到错误类型的 UITableViewCell

• 解决⽅方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始

化UITableViewCell的时候传⼊入⼀一个特定的字符串标识来设置reuseIdentifier(⼀一般 ⽤用UITableViewCell的类名)。

当UITableView要求dataSource返回UITableViewCell时,先 通过⼀一个字符串标识到对象池中查找对应类型的UITableViewCell对象,

如果有,就重 ⽤用,如果没有,就传⼊入这个字符串标识来初始化⼀一个UITableViewCell对象

Cell的重⽤用代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
// 1.定义⼀一个cell的标识
static NSString *ID = @"mjcell";
// 2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果缓存池中没有cell if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:ID];
}
// 4.设置cell的属性... return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: