最近编写新功能的时候,偶然间发现有较少开发经验的开发人员对于表视图的单元格的复用问题并非了解的很透彻,因此在此经过代码的形式快速的教给你们如何理解和运用单元格的复用问题。表视图是在开发中常常使用的控件,并且有时处理的内容量也是很是巨大的,这就须要考虑表视图的性能优化,而最基本的性能优化则是单元格的复用,正所谓基础打得好,才能飞得高,因此须要很好的理解单元格是如何复用的。xcode
在表视图显示的时候,会建立 (视图中可看的单元格个数+1)个单元格,一旦单元格由于滑动的而消失在咱们的视野中的时候,消失的单元格就会进入缓存池(或叫复用池),当有新的单元格须要显示的时候,会先从缓存池中取可用的单元格,获取成功则使用获取到的单元格,获取失败则从新建立心的单元格,这就是整个的复用机制。可是如何进行复用,这里有两种方式:缓存
第一种方式:性能优化
本身手动建立新的单元格ide
- #import "ViewController.h"
-
- #define width [UIScreen mainScreen].bounds.size.width
- #define height [UIScreen mainScreen].bounds.size.height
-
- static NSString *identifying = @"标识";
-
- @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
-
- @property (nonatomic, strong) UITableView *tableView;
-
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
-
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];
-
- _tableView.delegate = self;
- _tableView.dataSource = self;
- [self.view addSubview:_tableView];
- }
-
- #pragma mark - UITableViewDelegate 代理方法
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- return 100;
- }
-
- #pragma mark - UITableViewDataSource 数据源
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- return 20;
- }
-
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
-
- UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifying];
-
-
- if (cell == nil){
-
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifying];
- }
-
- cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
-
-
- NSLog(@"address --- %p ----- 第%ld行",cell, indexPath.row);
-
- return cell;
- }
-
- @end
经过打印的地址咱们能够看出复用:

第二种方式:post
经过注册单元格类的方式,由表视图本身建立单元格性能
- #import "ViewController.h"
-
- #define width [UIScreen mainScreen].bounds.size.width
- #define height [UIScreen mainScreen].bounds.size.height
-
- static NSString *identifying = @"标识";
-
- @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
-
- @property (nonatomic, strong) UITableView *tableView;
-
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
-
- _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];
-
- _tableView.delegate = self;
- _tableView.dataSource = self;
- [self.view addSubview:_tableView];
-
-
- [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifying];
- }
-
- #pragma mark - UITableViewDelegate 代理方法
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- return 100;
- }
-
- #pragma mark - UITableViewDataSource 数据源
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- return 20;
- }
-
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
-
- UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifying];
-
-
-
-
- cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
-
-
- NSLog(@"address --- %p ----- 第%ld行",cell, indexPath.row);
-
- return cell;
- }
-
- @end
经过打印的地址咱们能够看出复用:

两种单元格的复用的方式存在着细微的差异,新手通常状况下会不太理解,可是只要仔细观看以上的代码,或者将代码直接粘贴到新建立的工程中去,而后运行,能够得出和我截图出的同样的结果。但愿以上代码可以帮助对于表视图的单元格复用不太理解的开发人群。固然这里只是简单的讲解了一下单元格的重用机制,在实际的开发过程当中,可能会由于不一样的展现效果,须要合理的利用单元格的复用。学习