UITableViewHeaderFooterView 的使用和透明背景色的設(shè)置

環(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];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • UITableView的數(shù)據(jù)源(dataSource)和代理(delegate) UITableView需要一個(gè)數(shù)...
    Mr_天琦閱讀 5,090評(píng)論 0 9
  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風(fēng)格:UITa...
    蕭雪痕閱讀 2,990評(píng)論 2 10
  • #pragma mark someValueAboutTableView 1.tableView的樣式:UITab...
    瀟巖閱讀 1,050評(píng)論 0 0
  • 姓名:魏正君《六項(xiàng)精進(jìn)》第270期感謝2組 公司:綿陽大北農(nóng)農(nóng)牧科技有限公司 【日精進(jìn)打卡第87天】 【知~學(xué)習(xí)】...
    莫心莫肺閱讀 222評(píng)論 0 0
  • 據(jù)說女人都是水做的,跑場馬,連鞋子都擰得出來水,總得補(bǔ)回來,此生最愛茶當(dāng)酒歌,茶需獨(dú)飲,酒要群喝,馬拉松是越跑越...
    船的故事閱讀 935評(píng)論 5 1

友情鏈接更多精彩內(nèi)容