前言
關(guān)于UITableViewCell
和UICollectionViewCell
的復(fù)用想必大家已經(jīng)很清楚了,在此就不再啰嗦。不懂的朋友可以百度、Google吧甥绿!
然而,關(guān)于UITableViewHeaderFooterView之前就發(fā)現(xiàn)了有這樣API
, 如下:
// like dequeueReusableCellWithIdentifier:, but for headers/footers
- (UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier
發(fā)現(xiàn)后我就這樣使用:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// headerView復(fù)用
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderViewID"];
if (headerView == nil) {
headerView = [[RefreshHeaderView alloc] initWithReuseIdentifier:@"HeaderViewID"];
NSLog(@"section: %ld", (long)section);
}
return headerView;
}
其實(shí)當(dāng)時(shí)也就想:蘋(píng)果官方出了這樣的API就是希望我們復(fù)用UITableViewHeaderFooterView吧则披!
當(dāng)時(shí)也沒(méi)有多想共缕,沒(méi)有考慮過(guò)復(fù)用和不復(fù)用的區(qū)別。昨天和一個(gè)公司朋友討論關(guān)于這個(gè)問(wèn)題士复,今天就研究了下图谷,且看接下來(lái)的內(nèi)容。
正文
其實(shí)UITableViewHeaderFooterView
的原理與她們一樣阱洪。
- 復(fù)用的測(cè)試代碼如下:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// headerView復(fù)用測(cè)試情況
UITableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderViewID"];
if (headerView == nil) {
headerView = [[RefreshHeaderView alloc] initWithReuseIdentifier:@"HeaderViewID"];
NSLog(@"section: -------> %ld", (long)section);
}
return headerView;
}
---------------------------------
#import <UIKit/UIKit.h>
@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
- 復(fù)用的測(cè)試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
的朋友應(yīng)該知道從上面的數(shù)據(jù)中可以看出便贵,UITableViewHeaderFooterView
的復(fù)用原理完全與Cell
一致。
接下來(lái)我們看看不復(fù)用的測(cè)試代碼
- 不復(fù)用的測(cè)試代碼:
- (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;
}
接下來(lái)冗荸,我們看看大家最關(guān)心的內(nèi)存
和CPU
使用情況
- 不復(fù)用的內(nèi)存和CPU使用狀態(tài)
- 復(fù)用的內(nèi)存和CPU使用狀態(tài)
看了以上結(jié)果承璃,想必大家都明白了吧。
總結(jié):
-
UITableViewHeaderFooterView
復(fù)用原理與Cell
相同 -
UITableViewHeaderFooterView
由于復(fù)用機(jī)制蚌本,顯然在CPU使用上更節(jié)省绸硕。
總之,正如官方一樣魂毁,推薦大家使用UITableViewHeaderFooterView復(fù)用機(jī)制。
如果本人有不對(duì)的地方出嘹,希望朋友可以糾正席楚,謝謝!