UITableView 介绍


一、UITableView 理解

在移动应用中,能看到各式各样的表格数据。在iOS中,要实现表格数据的展示,最常用的做法就是使用UITableView。UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳。

UITableview有分组和不分组两种样式,可以在storyboard或者是用代码设置。

A、UITableViewStyleGrouped



B、UITableViewStylePlain


二、数据源


UITableView需要一个数据源(dataSource)来显示数据;
UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据;
没有设置数据源的UITableView只是个空壳;
凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView 的数据源。


数据展示过程:

@optional 可选实现
// 数据源一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;  

@required 必须实现
// 数据源每一组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// 每一行显示什么数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

三、UITableViewCell 

在UITableView中数据只有行的概念,并没有列的概念。UITableView中每行数据都是一个UITableViewCell,通过dataSource的 tableView:cellForRowAtIndexPath:方法来初始化每一行。在这个控件中为了显示更多的信息,iOS已经在其内部设置好了多个子控件以供开发者使用。UITableViewCell内部有个默认的子视图:contentView。

contentView是 UITableViewCell所显示内容的父视图,可显示一些辅助指示视图,通过设置UITableViewCell的 accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其他值如下:
UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton


UITableViewCellAccessoryDetailButton


UITableViewCellAccessoryCheckmark

此外,还可以通过cell的accessoryView属性来自定义辅助指示视图。


contentView下默认还有3个子视图:2个 UILabel 和 1个是 UIImageView,分别用来显示内容,详情和图片。通过UITableViewCell的 textLabel 和 detailTextLabel 和 imageView 属性访问)。



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

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
        UITableViewCellStyleDefault,
        UITableViewCellStyleValue1,
        UITableViewCellStyleValue2,
        UITableViewCellStyleSubtitle
};

这些子控件并不一定要全部使用,具体操作时可以通过UITableViewCellStyle进行设置:


四、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 = @”cell”;


// 2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];


// 3.如果缓存池中没有cell
 if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}


// 4.设置cell的属性...
return cell;

}


五、UITableView代理

上面我们已经看到通讯录的简单实现,但是我们发现单元格高度、分组标题高度以及尾部说明的高度都需要调整,此时就需要使用代理方法。UITableView代理方法有很多,例如监听单元格显示周期、监听单元格选择编辑操作、设置是否高亮显示单元格、设置行高等。

1. 设置行高:

#pragma mark - 代理方法
// 设置 cell 分组标题内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 50;
    }
    return 40;
}

// 设置 cell 每行高度(每行高度可以不一样)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

// 设置 cell 尾部说明内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}


2. 监听点击
通过方法 

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

方法可以实现点击 cell,将要处理的事情。


//待更新。














  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值