OC基础iOS进阶控件类

iOS回顾笔记(07) -- UITableView的使用和性

2017-03-21  本文已影响134人  xiaoyouPrince

如果问iOS中最重要的最常用的UI控件是什么,我觉得UITableView当之无愧!似乎所有常规APP都使用到了UITableView。下面就讲一讲UITableView的常用知识和使用原理及性能优化!

1.简介

Snip20170320_7.png

效果分别如图:

Snip20170320_8.png

UITableViewStylePlain:一种平铺的效果,并且分组组头默认有停靠效果;
UITableViewStyleGroup:一种组间分离的效果,每组之间间距较明显,有明显分组迹象。

2.数据源

说完了UITableView的简介,下面来说说他它是如何展示数据的。

  1. UITableView要展示数据必须设置数据源,没有数据源(DataSource)它就是一个空壳。
  2. UITableView会调用数据源方法来查询一共有多少行数据以及当前显示什么样的数据。
  3. 凡是遵守 UITableViewDelegate的OC对象都可以做UITableView的数据源

UITableView和数据源的关系如下

Snip20170320_9.png

数据源方法的调用过程

  1. 调用如下方法,查询一共有多少组数据

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented
    
  2. 调用如下方法,查询每一组一共有多少行数据

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
    
  3. 调用如下方法,查询每一行显示什么样的内容

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

UITableView还有一些其他的数据源方法如下

/**
 *  返回组头标题
 */
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
/**
 *  返回尾标题
 */
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

···

3.UITableViewCell的复用机制

3.1. 返回UITableViewCell的数据源方法的调用

最简单的返回UITableViewCell的方法如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];
    
    return cell;
}

但是实际使用过程中这样是非常耗费性能的,因为当用户滑动UITableView的时候,上面方法会疯狂的调用,疯狂的创建UITableViewCell。

苹果对这种情况做了一些性能优化,UITableViewCell在每次创建的时候只会创建当前UI页面上可见的Cell。当Cell滑动到屏幕外面,当前Cell会被放入到缓存池中,并且可以给Cell设置一个复用标识,当下次使用Cell的时候可以先到缓存池中查找,如果有对应复用标识的Cell就直接拿出来使用,并重新给内容赋值。

所以根据苹果复用Cell的思路我们在调用返回cell的方法思路应该如下:

所以上面方法从提高性能角度考虑,应该重新写为下面这样:

3.2. UITableView性能优化--Cell复用方式1

/**
 *  什么时候调用:每当有一个cell进入视野范围的时候调用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 0.创建cell复用标识
    static NSString *reuseIdentifier = @"cell";
    
    // 1.根据复用标识在复用池中进行查找
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    // 2.判断如果复用池cell为nil就根据复用标识自己创建
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    
    // 3.拿到cell进行数据覆盖
    cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];
    
    return cell;
}

3.2. UITableView性能优化--Cell复用方式2

我们可以手动给tableview注册对应标识的cell,保证从缓存池中能加载到对应标识的cell。

通过代码注册

// 注册cell
static NSString *reuseIdentifier = @"cell";
[tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:reuseIdentifier];

通过Xib注册

// 注册cell
static NSString *reuseIdentifier = @"cell"; // 标识可以在Xib中创建
    [tableview registerNib:[UINib nibWithNibName:@"对应的Xib名称" bundle:nil] forCellReuseIdentifier:reuseIdentifier];

在数据源方法中返回cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.根据复用标识在复用池中进行查找
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    // 2.拿到cell进行数据覆盖
    cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];
    
    return cell;
}

3.3 UITableView性能优化--Cell复用方式3

第三种方式是通过UITableViewController在StoryBoard中创建,见下面:5.UITableViewController

4.UITableView的代理方法

有了数据源方法,tableview就可以正常展示数据。有了对应的代理方法就可以正常监听tableview的事件操作。

下面列举一些常用代理方法:

/**
 返回行高,可根据不同行返回不同高
 
 @param tableView tableview
 @param indexPath 位置
 @return 行高
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

/**
 返回估计行高,此方法不进行真实计算,课改变Cell计算高度方法的调用顺序,
 
 @param tableView tableview
 @param indexPath 位置
 @return 行高
 */
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath;


/**
 cell的点击事件处理
 
 @param tableView tableview
 @param indexPath 位置
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

/**
 cell将要被选中 -- 先需取消选中某个cell才能正常选中新的cell
 */
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
/**
 cell将要被取消选中
 */
- (nullable NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath 

···

5.UITableViewController

UITableViewController继承自UIViewController。可以方便创建有tableview的控制器

{
// 1.会默认去缓存池中进行查找,如果没有自动去StoryBaord中找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

// 2.拿到cell进行数据覆盖
cell.textLabel.text = [NSString stringWithFormat:@"第 %zd 行",indexPath.row];

return cell;

}

```

**通过StoryBoard创建Cell更加方便**

小结

上一篇 下一篇

猜你喜欢

热点阅读