TableView的熟练使用

UITableView简单认识
什么是UITableView
在应用中能看到各式各样的列表数据

在iOS中要想展示列表数据,最常用的就是用UITableView
UITableView继承自UIScrollView,因此支持垂直滚动,并且性能极佳
UITableView的样式
单组样式 UITableViewStylePlain
多组样式 UITableViewStyleGrouped

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

UITbaleView展示数据的过程

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

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

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

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

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

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

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

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

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

- (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;

设置每一组头部的高度
self.tableView.sectionHeaderHeight = 80;

设置每一组尾部的高度
self.tableView.sectionFooterHeight = 80;

设置分割线的颜色
self.tableView.separatorColor = [UIColor clearColor] ;

设置分割线的样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

设置tableView表头view
self.tableView.tableHeaderView = [[UISwitch alloc] init];

设置tableView表尾view
self.tableView.tableFooterView = [[UISwitch alloc] init];

设置cell右边的指示控件
// accessoryView的优先级 > accessoryType
cell.accessoryView = [[UISwitch alloc] init];

设置cell右边的指示样式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

设置cell的选中样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

设置cell的背景颜色
cell.backgroundColor = [UIColor redColor];

设置cell选中的背景view
UIView *selectedView = [[UIView alloc] init];
selectedView.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectedView;

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

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

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

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

返回的每一组的头部标题

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

返回的每一组的尾部标题

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

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

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

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

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

返回的每一行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

每当一个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;
}

另外一种方法
注册某个重用标示对应的cell类型(只会执行一次)
一般用于自定义cell

- (void)viewDidLoad {
    [super viewDidLoad];

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

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容