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)容合作請聯(lián)系作者
  • 序言:七十年代末绢掰,一起剝皮案震驚了整個(gè)濱河市辕狰,隨后出現(xiàn)的幾起案子毅戈,更是在濱河造成了極大的恐慌,老刑警劉巖下翎,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異朽寞,居然都是意外死亡挂洛,警方通過查閱死者的電腦和手機(jī)黎比,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鸳玩,“玉大人阅虫,你說我怎么就攤上這事〔桓” “怎么了颓帝?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長窝革。 經(jīng)常有香客問我购城,道長,這世上最難降的妖魔是什么虐译? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任瘪板,我火速辦了婚禮,結(jié)果婚禮上漆诽,老公的妹妹穿的比我還像新娘侮攀。我一直安慰自己,他們只是感情好厢拭,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布兰英。 她就那樣靜靜地躺著,像睡著了一般供鸠。 火紅的嫁衣襯著肌膚如雪畦贸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天楞捂,我揣著相機(jī)與錄音薄坏,去河邊找鬼。 笑死寨闹,一個(gè)胖子當(dāng)著我的面吹牛颤殴,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鼻忠,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼涵但,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了帖蔓?” 一聲冷哼從身側(cè)響起矮瘟,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎塑娇,沒想到半個(gè)月后澈侠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡埋酬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年哨啃,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了烧栋。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,040評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拳球,死狀恐怖审姓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情祝峻,我是刑警寧澤魔吐,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站莱找,受9級特大地震影響酬姆,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜奥溺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一辞色、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧浮定,春花似錦相满、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽棕所。三九已至闸盔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間琳省,已是汗流浹背迎吵。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留针贬,地道東北人击费。 一個(gè)月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像桦他,于是被迫代替她去往敵國和親蔫巩。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容

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