iOS开发之UI学习-UITableView的复用机制

 

iOS开发之UI学习-UITableView的复用机制

标签: iosUI控件UITableView单元格复用重用
 分类:

    最近编写新功能的时候,偶然间发现有较少开发经验的开发人员对于表视图的单元格的复用问题并非了解的很透彻,因此在此经过代码的形式快速的教给你们如何理解和运用单元格的复用问题。表视图是在开发中常常使用的控件,并且有时处理的内容量也是很是巨大的,这就须要考虑表视图的性能优化,而最基本的性能优化则是单元格的复用,正所谓基础打得好,才能飞得高,因此须要很好的理解单元格是如何复用的。xcode

 

    在表视图显示的时候,会建立 (视图中可看的单元格个数+1)个单元格,一旦单元格由于滑动的而消失在咱们的视野中的时候,消失的单元格就会进入缓存池(或叫复用池),当有新的单元格须要显示的时候,会先从缓存池中取可用的单元格,获取成功则使用获取到的单元格,获取失败则从新建立心的单元格,这就是整个的复用机制。可是如何进行复用,这里有两种方式:缓存

 

第一种方式:性能优化

本身手动建立新的单元格ide

 

[objc]  view plain  copy
 
  1. #import "ViewController.h"  
  2.   
  3. #define width  [UIScreen mainScreen].bounds.size.width  
  4. #define height [UIScreen mainScreen].bounds.size.height  
  5.   
  6. static NSString *identifying = @"标识";  
  7.   
  8. @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>  
  9.   
  10. @property (nonatomic, strong) UITableView *tableView;  
  11.   
  12. @end  
  13.   
  14. @implementation ViewController  
  15.   
  16. - (void)viewDidLoad {  
  17.     [super viewDidLoad];  
  18.       
  19.     //建立表视图  
  20.     /* 
  21.      UITableViewStylePlain     平铺类型 
  22.      UITableViewStyleGrouped   分组类型 
  23.      */  
  24.     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];  
  25.     //让当前控制器成为表视图的代理  
  26.     _tableView.delegate = self;  
  27.     _tableView.dataSource = self;  
  28.     [self.view addSubview:_tableView];  
  29. }  
  30.   
  31. #pragma mark - UITableViewDelegate 代理方法  
  32. //设置cell的行高  
  33. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  34.       
  35.     return 100;  
  36. }  
  37.   
  38. #pragma mark - UITableViewDataSource 数据源  
  39. //设置cell的个数  
  40. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  41.       
  42.     return 20;  
  43. }  
  44.   
  45. //设置cell  
  46. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  47.       
  48.     //1.根据标识去缓存池中去取cell  
  49.     UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifying];  
  50.       
  51.     //2.根据是否取到了可用的cell来判断是否须要从新建立cell(手动建立新的单元格)  
  52.     if (cell == nil){  
  53.           
  54.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifying];  
  55.     }  
  56.     //3.设置单元格的显示数据  
  57.     cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];  
  58.       
  59.     //经过打印cell的地址,咱们来查看是否复用了cell  
  60.     NSLog(@"address --- %p ----- 第%ld行",cell, indexPath.row);  
  61.       
  62.     return cell;  
  63. }  
  64.   
  65. @end  

经过打印的地址咱们能够看出复用:

 

 

 

 

 

第二种方式:post

经过注册单元格类的方式,由表视图本身建立单元格性能

 

[objc]  view plain  copy
 
  1. #import "ViewController.h"  
  2.   
  3. #define width  [UIScreen mainScreen].bounds.size.width  
  4. #define height [UIScreen mainScreen].bounds.size.height  
  5.   
  6. static NSString *identifying = @"标识";  
  7.   
  8. @interface ViewController ()<UITableViewDelegate, UITableViewDataSource>  
  9.   
  10. @property (nonatomic, strong) UITableView *tableView;  
  11.   
  12. @end  
  13.   
  14. @implementation ViewController  
  15.   
  16. - (void)viewDidLoad {  
  17.     [super viewDidLoad];  
  18.       
  19.     //建立表视图  
  20.     /* 
  21.      UITableViewStylePlain     平铺类型 
  22.      UITableViewStyleGrouped   分组类型 
  23.      */  
  24.     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];  
  25.     //让当前控制器成为表视图的代理  
  26.     _tableView.delegate = self;  
  27.     _tableView.dataSource = self;  
  28.     [self.view addSubview:_tableView];  
  29.       
  30.     //注册单元格的类型  
  31.     [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifying];  
  32. }  
  33.   
  34. #pragma mark - UITableViewDelegate 代理方法  
  35. //设置cell的行高  
  36. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
  37.   
  38.     return 100;  
  39. }  
  40.   
  41. #pragma mark - UITableViewDataSource 数据源  
  42. //设置cell的个数  
  43. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
  44.   
  45.     return 20;  
  46. }  
  47.   
  48. //设置cell  
  49. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
  50.   
  51.     //1.根据标识去缓存池中去取cell  
  52.     UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifying];  
  53.   
  54.     //2.假若根据ID标识来判断有没有对应的cell类型,当缓存池中没有能够复用的cell的时候,会根据注册的类型自动建立cell  
  55.       
  56.     //3.设置单元格的显示数据  
  57.     cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];  
  58.       
  59.     //经过打印cell的地址,咱们来查看是否复用了cell  
  60.     NSLog(@"address --- %p ----- 第%ld行",cell, indexPath.row);  
  61.       
  62.     return cell;  
  63. }  
  64.   
  65. @end  

经过打印的地址咱们能够看出复用:

 

 

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

相关文章
相关标签/搜索