iOS tableView的sectionHeader點(diǎn)擊折疊方法

foldUp

foldDown

sectionHeader【帶按鈕的down-up】-明明創(chuàng)建了芋忿,但點(diǎn)擊刷新后那個(gè)煩人的按鈕總是不乖

試過好多次辦法:

1嵌施、懶加載只寫一次方法漠吻,可以讓按鈕正常折疊展開擅耽,但是header只有一個(gè)

2傻盟、用可變字典記錄headerView速蕊,只加載一次嫂丙,親測(cè)不行

3娘赴、經(jīng)調(diào)查發(fā)現(xiàn)tableView 的header依然可以復(fù)用,那何不重寫headerFooterView的方法呢跟啤,于是乎

重點(diǎn)來啦

tableView的headerView和cell有些類似诽表, 所以先創(chuàng)建一個(gè)類, 繼承自UITableViewHeaderFooterView

.h中

首先明確headerView需要暴露在外部的接口和headerView的大體形態(tài)隅肥,我做的是在sectionHeaderView上有一個(gè)標(biāo)題titleLabel

一個(gè)明細(xì)detailLabel 一個(gè)表示上下的圖片竿奏,

一個(gè)點(diǎn)擊section響應(yīng)的按鈕。其次在外部要能改變section的折疊狀態(tài)腥放。當(dāng)section折疊狀態(tài)改變時(shí)需要穿到外部進(jìn)行cellNumber的改變泛啸。所以需要一個(gè)protocol。

@protocol FoldSectionHeaderViewDelegate <NSObject>

- (void)foldHeaderInSection:(NSInteger)SectionHeader;

@end

聲明外部可用的屬性:

@property(nonatomic, assign) BOOL fold;/**< 是否折疊 */

@property(nonatomic, assign) NSInteger section;/**< 選中的section */

@property(nonatomic, weak) id<FoldSectionHeaderViewDelegate> delegate;/**< 代理 */

創(chuàng)建sectionHeaderView上控件的方法

- (void)setFoldSectionHeaderViewWithTitle:(NSString *)title detail:(NSString *)detail type:(HerderStyle)type section:(NSInteger)section canFold:(BOOL)canFold;

.m 中

聲明內(nèi)部全局變量

@implementation LGJFoldHeaderView

{

? ? BOOL _created;/**< 是否創(chuàng)建過 */

? ? UILabel *_titleLabel;/**< 標(biāo)題 */

? ? UILabel *_detailLabel;/**< 其他內(nèi)容 */

? ? UIImageView *_imageView;/**< 圖標(biāo) */

? ? UIButton *_btn;/**< 收起按鈕 */

? ? BOOL _canFold;/**< 是否可展開 */

}

實(shí)現(xiàn)方法

- (void)setFoldSectionHeaderViewWithTitle:(NSString *)title detail:(NSString *)detail type:(HerderStyle)type section:(NSInteger)section canFold:(BOOL)canFold {

? ? if (!_created) {

? ? ? ? [self creatUI];

? ? }

? ? _titleLabel.text = title;

? ? if (type == HerderStyleNone) {

? ? ? ? _detailLabel.hidden = YES;

? ? } else {

? ? ? ? _detailLabel.hidden = NO;

? ? ? ? _detailLabel.attributedText = [self attributeStringWith:detail];

? ? }

? ? _section = section;

? ? _canFold = canFold;

? ? if (canFold) {

? ? ? ? _imageView.hidden = NO;

? ? } else {

? ? ? ? _imageView.hidden = YES;

? ? }

}

//改變detail上的字體顏色

- (NSMutableAttributedString *)attributeStringWith:(NSString *)money {

? ? NSString *str = [NSString stringWithFormat:@"應(yīng)收合計(jì):%@", money];

? ? NSMutableAttributedString *ats = [[NSMutableAttributedString alloc] initWithString:str];

? ? NSRange range = [str rangeOfString:money];

? ? [ats setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} range:range];

? ? return ats;

}

//creatUI

- (void)creatUI {

? ? _created = YES;

? ? //標(biāo)題

? ? _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 90, 30)];

? ? _titleLabel.backgroundColor = [UIColor grayColor];

? ? [self.contentView addSubview:_titleLabel];

? ? //其他內(nèi)容

? ? _detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(105, 5, 320-40, 30)];

? ? _detailLabel.backgroundColor = [UIColor greenColor];

? ? [self.contentView addSubview:_detailLabel];

? ? //按鈕

? ? _btn = [UIButton buttonWithType:UIButtonTypeCustom];

? ? _btn.frame = CGRectMake(0, 0, 320, 30);

? ? [_btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

? ? [self.contentView addSubview:_btn];

? ? //圖片

? ? _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 - 30, 15, 8, 9)];

? ? _imageView.image = [UIImage imageNamed:@"arrow_down_gray"];

? ? [self.contentView addSubview:_imageView];

? ? //線

? ? UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 39, 320, 1)];

? ? line.image = [UIImage imageNamed:@"line"];

? ? [self.contentView addSubview:line];

}

//重寫fold的set方法秃症, 根據(jù)fold的狀態(tài)候址, 改變圖片形狀

- (void)setFold:(BOOL)fold {

? ? _fold = fold;

? ? if (fold) {

? ? ? ? _imageView.image = [UIImage imageNamed:@"arrow_down_gray"];

? ? } else {

? ? ? ? _imageView.image = [UIImage imageNamed:@"arrow_up_gray"];

? ? }

}

//按鈕的點(diǎn)擊響應(yīng)方法,將代理傳出

- (void)btnClick:(UIButton *)btn {

? ? if (_canFold) {

? ? ? ? if ([self.delegate respondsToSelector:@selector(foldHeaderInSection:)]) {

? ? ? ? ? ? [self.delegate foldHeaderInSection:_section];

? ? ? ? }

? ? }

}

《2》在viewController中的使用時(shí)种柑,引入頭文件岗仑,遵循代理

在controller中,我們需要一個(gè)字典來記錄section的折疊狀態(tài)聚请,這個(gè)字典根據(jù)當(dāng)前選中的第幾個(gè)section和section的狀態(tài)來記錄【也可以在controller里面添加一個(gè)可變數(shù)組來記錄狀態(tài)0-1】

_foldInfoDic = [NSMutableDictionary dictionaryWithDictionary:@{

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"0":@"1",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"1":@"1",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"2":@"1",

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @"3":@"0"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];

這里我創(chuàng)建4個(gè)section荠雕,前三個(gè)的默認(rèn)狀態(tài)是1,說明此時(shí)section是打開的驶赏。

在numberOfRowsInSection這個(gè)方法中炸卑,根據(jù)當(dāng)前section轉(zhuǎn)化成字符串當(dāng)做key(因?yàn)槲覀円婚_始字典創(chuàng)建的思路就是根據(jù)對(duì)應(yīng)的section和開關(guān)狀態(tài)來創(chuàng)建的)來查找字典中的value

并將value轉(zhuǎn)化成bool類型,說明只有開關(guān)兩種狀態(tài)煤傍。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

? ? NSString *key = [NSString stringWithFormat:@"%d", (int)section];

? ? BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];

? ? if (section == 0) {

? ? ? ? return folded?_arr.count:0;

? ? } else if (section == 1) {

? ? ? ? return folded?_arr.count:0;

? ? } else if (section == 2) {

? ? ? ? return folded?_arr.count:0;

? ? } else {

? ? ? ? return folded?_arr.count:0;

? ? }

}

在viewForHeaderInSection方法中 和cell類似盖文,根據(jù)identifier先從池子中找headerView

如果沒有就創(chuàng)建,創(chuàng)建時(shí)記得給headerView加identifier患久。創(chuàng)建好之后根據(jù)當(dāng)前section

個(gè)性定制sectionHeaderView上面的內(nèi)容椅寺。將headerView的delegate= self;

在headerView創(chuàng)建好的這時(shí)候浑槽,改變字典中的值。

NSString *key = [NSString stringWithFormat:@"%d", (int)section];

? ? BOOL folded = [[_foldInfoDic valueForKey:key] boolValue];

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

? ? LGJFoldHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];

? ? if (!headerView) {

? ? ? ? headerView = [[LGJFoldHeaderView alloc] initWithReuseIdentifier:@"header"];

? ? }

? ? if (section == 0) {

? ? ? ? [headerView setFoldSectionHeaderViewWithTitle:@"我是第一個(gè)Section" detail:@"9999" type: HerderStyleTotal section:0 canFold:YES];

? ? } else if (section == 1) {

? ? ? ? [headerView setFoldSectionHeaderViewWithTitle:@"我是第二個(gè)Section" detail:@"8888" type:HerderStyleTotal section:1 canFold:YES];

? ? } else if (section == 2){

? ? ? ? [headerView setFoldSectionHeaderViewWithTitle:@"我是第三個(gè)Section" detail:nil type:HerderStyleNone section:2 canFold:YES];

? ? } else {

? ? ? ? [headerView setFoldSectionHeaderViewWithTitle:@"我是第四個(gè)Seciton" detail:@"777" type:HerderStyleTotal section:3 canFold:NO];

? ? }

? ? headerView.delegate = self;

? ? NSString *key = [NSString stringWithFormat:@"%d", (int)section];

? ? BOOL folded = [[_foldInfoDic valueForKey:key] boolValue];

? ? headerView.fold = folded;

? ? return headerView;

}

實(shí)現(xiàn)headerView的代理方法

- (void)foldHeaderInSection:(NSInteger)SectionHeader {

? ? NSString *key = [NSString stringWithFormat:@"%d",(int)SectionHeader];

? ? BOOL folded = [[_foldInfoDic objectForKey:key] boolValue];

? ? NSString *fold = folded ? @"0" : @"1";

? ? [_foldInfoDic setValue:fold forKey:key];

? ? NSMutableIndexSet *set = [[NSMutableIndexSet alloc] initWithIndex:SectionHeader];

? ? [_tableView reloadSections:set withRowAnimation:UITableViewRowAnimationLeft];

}

好了返帕,跳過的坑希望可以幫助更多人不被踩M┎!!>S镊靴!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市链韭,隨后出現(xiàn)的幾起案子偏竟,更是在濱河造成了極大的恐慌,老刑警劉巖敞峭,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件踊谋,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡旋讹,警方通過查閱死者的電腦和手機(jī)殖蚕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沉迹,“玉大人睦疫,你說我怎么就攤上這事”夼唬” “怎么了蛤育?”我有些...
    開封第一講書人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)葫松。 經(jīng)常有香客問我瓦糕,道長(zhǎng),這世上最難降的妖魔是什么进宝? 我笑而不...
    開封第一講書人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任刻坊,我火速辦了婚禮,結(jié)果婚禮上党晋,老公的妹妹穿的比我還像新娘谭胚。我一直安慰自己,他們只是感情好未玻,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開白布灾而。 她就那樣靜靜地躺著,像睡著了一般扳剿。 火紅的嫁衣襯著肌膚如雪旁趟。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,736評(píng)論 1 312
  • 那天庇绽,我揣著相機(jī)與錄音锡搜,去河邊找鬼橙困。 笑死,一個(gè)胖子當(dāng)著我的面吹牛耕餐,可吹牛的內(nèi)容都是我干的凡傅。 我是一名探鬼主播,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼肠缔,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼夏跷!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起明未,我...
    開封第一講書人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤槽华,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后趟妥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體猫态,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年煮纵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了懂鸵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片偏螺。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡行疏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出套像,到底是詐尸還是另有隱情酿联,我是刑警寧澤,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布夺巩,位于F島的核電站贞让,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏柳譬。R本人自食惡果不足惜喳张,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望美澳。 院中可真熱鬧销部,春花似錦、人聲如沸制跟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽雨膨。三九已至擂涛,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間聊记,已是汗流浹背撒妈。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工恢暖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人狰右。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓胀茵,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親挟阻。 傳聞我的和親對(duì)象是個(gè)殘疾皇子琼娘,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

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

  • 效果 如圖: 在項(xiàng)目中遇到這個(gè)問題,總結(jié)出比較方便使用的方法 《1》tableView的headerView和ce...
    劉光軍_MVP閱讀 16,244評(píng)論 16 83
  • 一附鸽、簡(jiǎn)介 <<UITableView(或簡(jiǎn)單地說脱拼,表視圖)的一個(gè)實(shí)例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,614評(píng)論 3 3
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子坷备,類似...
    liudhkk閱讀 9,067評(píng)論 3 38
  • 1.badgeVaule氣泡提示 2.git終端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夾內(nèi)容...
    i得深刻方得S閱讀 4,675評(píng)論 1 9
  • 老弟省撑,95年出生赌蔑,21歲,標(biāo)準(zhǔn)的90后竟秫。 喜歡動(dòng)漫娃惯、喜歡玄幻、喜歡網(wǎng)游肥败、喜歡吃.... 所以你能想到一個(gè)180的2...
    S菩提只吃半碗飯閱讀 460評(píng)論 0 2