環(huán)境 iOS 9.3 下測試 2016-06-16
我們知道UITableView有代理方法設(shè)置每個(gè)section的headerview和footerview
為了減少UITableView的內(nèi)存開銷
Apple引入了UITableViewHeaderFooterView這個(gè)類 這個(gè)類是在iOS 6 的時(shí)候加入 和UITableViewCell這個(gè)類一樣 這個(gè)類由reuseIdentifier 這個(gè)概念
所以初始化方法也和UITableViewCell類似了
直接上代碼
Apple 關(guān)于UITableViewHeaderFooterView的定義
#import <Foundation/Foundation.h>
#import <UIKit/UIView.h>
#import <UIKit/UITableView.h>
// Either the header or footer for a section
NS_ASSUME_NONNULL_BEGIN
NS_CLASS_AVAILABLE_IOS(6_0) @interface UITableViewHeaderFooterView : UIView
- (instancetype)initWithReuseIdentifier:(nullable NSString *)reuseIdentifier NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@property (nonatomic, strong, null_resettable) UIColor *tintColor;
@property (nonatomic, readonly, strong, nullable) UILabel *textLabel;
@property (nonatomic, readonly, strong, nullable) UILabel *detailTextLabel; // only supported for headers in grouped style
@property (nonatomic, readonly, strong) UIView *contentView;
@property (nonatomic, strong, nullable) UIView *backgroundView;
@property (nonatomic, readonly, copy, nullable) NSString *reuseIdentifier;
- (void)prepareForReuse; // if the view is reusable (has a reuse identifier), this is called just before the view is returned from the table view method dequeueReusableHeaderFooterViewWithIdentifier:. If you override, you MUST call super.
@end
NS_ASSUME_NONNULL_END
實(shí)際應(yīng)用
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (tableView.tag == 100) {
static NSString *tableViewHeaderViewIdentifier = @"tableViewHeaderViewIdentifier";
UITableViewHeaderFooterView *headView = [tableView tableViewHeaderViewIdentifier];
if (!headView) {
headView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:tableViewHeaderViewIdentifier];
}
UILabel *tableHeaderLabel = [headView viewWithTag:10001];
if (!tableHeaderLabel) {
tableHeaderLabel = [[UILabel alloc]init];
tableHeaderLabel.tag = 10001;
tableHeaderLabel.frame = (CGRect){20,0,WIDTH(tableView)-40,40};
tableHeaderLabel.font = Font(inchesForValue(16, 18));
tableHeaderLabel.textColor = RGBColor(26, 161, 230);
tableHeaderLabel.textAlignment = NSTextAlignmentCenter;
[headView.contentView addSubview:tableHeaderLabel];
}
tableHeaderLabel.text = @"title";
UIView *lineView = [headView viewWithTag:10002];
if (!lineView) {
lineView = [[UIView alloc] init];
lineView.tag = 10002;
lineView.frame = (CGRect){0,0,WIDTH(tableView),0.5};
lineView.backgroundColor = RGBAColor(118, 134, 147, 0.2);
[headView addSubview:lineView];
}
UIView *bottomLineView = [headView viewWithTag:10003];
if (!bottomLineView) {
bottomLineView = [[UIView alloc] init];
bottomLineView.tag = 10003;
bottomLineView.frame = (CGRect){0,HEIGHT(tableHeaderLabel)-0.5,WIDTH(tableView),0.5};
bottomLineView.backgroundColor = RGBAColor(118, 134, 147, 0.2);
[headView addSubview:bottomLineView];
}
return headView;
}
return nil;
}
這樣設(shè)置之后 會(huì)發(fā)現(xiàn)apple給的默認(rèn)的UITableViewHeaderFooterView的背景顏色是灰色
如果我們要改成透明顏色
這時(shí)候就要加入這個(gè)代理了
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isMemberOfClass:[UITableViewHeaderFooterView class]]) {
((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor clearColor];
}
}