前言
关于UITableViewCell
和UICollectionViewCell
的复用想必大家已经很清楚了,在此就不再啰嗦。不懂的朋友可以百度、Google吧!
然而,关于UITableViewHeaderFooterView之前就发现了有这样API
, 如下:
// like dequeueReusableCellWithIdentifier:, but for headers/footers
- (UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
发现后我就这样使用:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// headerView复用
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderViewID"];
if (headerView == nil) {
headerView = [[RefreshHeaderView alloc] initWithReuseIdentifier:@"HeaderViewID"];
NSLog(@"section: %ld", (long)section);
}
return headerView;
}
其实当时也就想:苹果官方出了这样的API就是希望我们复用UITableViewHeaderFooterView吧!
当时也没有多想,没有考虑过复用和不复用的区别。昨天和一个公司朋友讨论关于这个问题,今天就研究了下,且看接下来的内容。
正文
其实UITableViewHeaderFooterView
的原理与她们一样。
- 复用的测试代码如下:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// headerView复用测试情况
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderViewID"];
if (headerView == nil) {
headerView = [[RefreshHeaderView alloc] initWithReuseIdentifier:@"HeaderViewID"];
NSLog(@"section: -------> %ld", (long)section);
}
return headerView;
}
---------------------------------
#import
@interface RefreshHeaderView : UITableViewHeaderFooterView
@end
#import "RefreshHeaderView.h"
@interface RefreshHeaderView ()
/**image*/
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation RefreshHeaderView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
self.contentView.backgroundColor = [UIColor whiteColor];
self.imageView = [[UIImageView alloc] init];
self.imageView.frame = CGRectMake(10, 10, 30, 30);
self.imageView.image = [UIImage imageNamed:@"stock"];
[self.contentView addSubview:self.imageView];
for (int i = 0; i < 10; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50 + i * 20, 0, 100, 50)];
label.text = @"呵呵";
[self.contentView addSubview:label];
}
}
return self;
}
@end
- 复用的测试Log如下:
2017-01-19 09:38:07.516709 QJNetworking[4169:1311469] section: -------> 0
2017-01-19 09:38:07.518637 QJNetworking[4169:1311469] section: -------> 1
2017-01-19 09:38:07.519517 QJNetworking[4169:1311469] section: -------> 2
2017-01-19 09:38:07.520233 QJNetworking[4169:1311469] section: -------> 3
2017-01-19 09:38:07.520863 QJNetworking[4169:1311469] section: -------> 4
2017-01-19 09:38:07.521519 QJNetworking[4169:1311469] section: -------> 5
2017-01-19 09:38:07.522134 QJNetworking[4169:1311469] section: -------> 6
2017-01-19 09:38:07.522838 QJNetworking[4169:1311469] section: -------> 7
2017-01-19 09:38:07.523539 QJNetworking[4169:1311469] section: -------> 8
2017-01-19 09:38:07.524204 QJNetworking[4169:1311469] section: -------> 9
2017-01-19 09:38:07.524827 QJNetworking[4169:1311469] section: -------> 10
2017-01-19 09:38:07.525417 QJNetworking[4169:1311469] section: -------> 11
2017-01-19 09:38:07.526031 QJNetworking[4169:1311469] section: -------> 12
2017-01-19 09:38:11.855421 QJNetworking[4169:1311469] section: -------> 13
2017-01-19 09:38:11.963743 QJNetworking[4169:1311469] section: -------> 14
2017-01-19 09:38:12.986768 QJNetworking[4169:1311469] section: -------> 23
2017-01-19 09:38:13.520631 QJNetworking[4169:1311469] section: -------> 29
2017-01-19 09:38:15.380638 QJNetworking[4169:1311469] section: -------> 57
2017-01-19 09:38:15.480633 QJNetworking[4169:1311469] section: -------> 62
2017-01-19 09:38:15.547375 QJNetworking[4169:1311469] section: -------> 65
2017-01-19 09:38:15.635934 QJNetworking[4169:1311469] section: -------> 68
2017-01-19 09:38:16.047329 QJNetworking[4169:1311469] section: -------> 80
2017-01-19 09:38:16.097419 QJNetworking[4169:1311469] section: -------> 82
2017-01-19 09:38:16.180931 QJNetworking[4169:1311469] section: -------> 85
2017-01-19 09:38:17.654964 QJNetworking[4169:1311469] section: -------> 85
2017-01-19 09:38:17.729023 QJNetworking[4169:1311469] section: -------> 82
2017-01-19 09:38:17.812837 QJNetworking[4169:1311469] section: -------> 79
2017-01-19 09:38:18.281034 QJNetworking[4169:1311469] section: -------> 68
2017-01-19 09:38:18.365286 QJNetworking[4169:1311469] section: -------> 65
2017-01-19 09:38:18.480834 QJNetworking[4169:1311469] section: -------> 62
2017-01-19 09:38:18.997506 QJNetworking[4169:1311469] section: -------> 51
2017-01-19 09:38:19.114332 QJNetworking[4169:1311469] section: -------> 48
2017-01-19 09:38:19.469727 QJNetworking[4169:1311469] section: -------> 41
2017-01-19 09:38:19.553230 QJNetworking[4169:1311469] section: -------> 38
2017-01-19 09:38:19.680752 QJNetworking[4169:1311469] section: -------> 30
2017-01-19 09:38:19.747531 QJNetworking[4169:1311469] section: -------> 27
2017-01-19 09:38:19.797485 QJNetworking[4169:1311469] section: -------> 25
2017-01-19 09:38:19.849612 QJNetworking[4169:1311469] section: -------> 23
2017-01-19 09:38:20.068577 QJNetworking[4169:1311469] section: -------> 17
2017-01-19 09:38:20.118201 QJNetworking[4169:1311469] section: -------> 15
2017-01-19 09:38:20.164471 QJNetworking[4169:1311469] section: -------> 13
2017-01-19 09:38:20.179870 QJNetworking[4169:1311469] section: -------> 11
2017-01-19 09:38:20.196951 QJNetworking[4169:1311469] section: -------> 9
2017-01-19 09:38:20.213055 QJNetworking[4169:1311469] section: -------> 7
2017-01-19 09:38:20.249866 QJNetworking[4169:1311469] section: -------> 4
2017-01-19 09:38:20.262628 QJNetworking[4169:1311469] section: -------> 2
2017-01-19 09:38:20.833058 QJNetworking[4169:1311469] section: -------> 12
了解Cell
的朋友应该知道从上面的数据中可以看出,UITableViewHeaderFooterView
的复用原理完全与Cell
一致。
接下来我们看看不复用的测试代码
- 不复用的测试代码:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] init];
NSLog(@"section: -------> %ld", (long)section);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
imageView.image = [UIImage imageNamed:@"stock"];
[view addSubview:imageView];
for (int i = 0; i < 10; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50 + i * 20, 0, 100, 50)];
label.text = @"呵呵";
[view addSubview:label];
}
return view;
}
接下来,我们看看大家最关心的内存
和CPU
使用情况
- 不复用的内存和CPU使用状态
- 复用的内存和CPU使用状态
看了以上结果,想必大家都明白了吧。
总结:
-
UITableViewHeaderFooterView
复用原理与Cell
相同 -
UITableViewHeaderFooterView
由于复用机制,显然在CPU使用上更节省。
总之,正如官方一样,推荐大家使用UITableViewHeaderFooterView复用机制。
如果本人有不对的地方,希望朋友可以纠正,谢谢!