iOS tableView的sectionHeader点击折叠方法

foldUp

foldDown

sectionHeader【带按钮的down-up】-明明创建了,但点击刷新后那个烦人的按钮总是不乖

试过好多次办法:

1、懒加载只写一次方法,可以让按钮正常折叠展开,但是header只有一个

2、用可变字典记录headerView,只加载一次,亲测不行

3、经调查发现tableView 的header依然可以复用,那何不重写headerFooterView的方法呢,于是乎

重点来啦

tableView的headerView和cell有些类似, 所以先创建一个类, 继承自UITableViewHeaderFooterView

.h中

首先明确headerView需要暴露在外部的接口和headerView的大体形态,我做的是在sectionHeaderView上有一个标题titleLabel

一个明细detailLabel 一个表示上下的图片,

一个点击section响应的按钮。其次在外部要能改变section的折叠状态。当section折叠状态改变时需要穿到外部进行cellNumber的改变。所以需要一个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;/**< 代理 */

创建sectionHeaderView上控件的方法

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

.m 中

声明内部全局变量

@implementation LGJFoldHeaderView

{

    BOOL _created;/**< 是否创建过 */

    UILabel *_titleLabel;/**< 标题 */

    UILabel *_detailLabel;/**< 其他内容 */

    UIImageView *_imageView;/**< 图标 */

    UIButton *_btn;/**< 收起按钮 */

    BOOL _canFold;/**< 是否可展开 */

}

实现方法

- (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:@"应收合计:%@", 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;

    //标题

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

    _titleLabel.backgroundColor = [UIColor grayColor];

    [self.contentView addSubview:_titleLabel];

    //其他内容

    _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方法, 根据fold的状态, 改变图片形状

- (void)setFold:(BOOL)fold {

    _fold = fold;

    if (fold) {

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

    } else {

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

    }

}

//按钮的点击响应方法,将代理传出

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

    if (_canFold) {

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

            [self.delegate foldHeaderInSection:_section];

        }

    }

}

《2》在viewController中的使用时,引入头文件,遵循代理

在controller中,我们需要一个字典来记录section的折叠状态,这个字典根据当前选中的第几个section和section的状态来记录【也可以在controller里面添加一个可变数组来记录状态0-1】

_foldInfoDic = [NSMutableDictionary dictionaryWithDictionary:@{

                                                                  @"0":@"1",

                                                                  @"1":@"1",

                                                                  @"2":@"1",

                                                                  @"3":@"0"

                                                                  }];

这里我创建4个section,前三个的默认状态是1,说明此时section是打开的。

在numberOfRowsInSection这个方法中,根据当前section转化成字符串当做key(因为我们一开始字典创建的思路就是根据对应的section和开关状态来创建的)来查找字典中的value

并将value转化成bool类型,说明只有开关两种状态。

- (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类似,根据identifier先从池子中找headerView

如果没有就创建,创建时记得给headerView加identifier。创建好之后根据当前section

个性定制sectionHeaderView上面的内容。将headerView的delegate= self;

在headerView创建好的这时候,改变字典中的值。

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:@"我是第一个Section" detail:@"9999" type: HerderStyleTotal section:0 canFold:YES];

    } else if (section == 1) {

        [headerView setFoldSectionHeaderViewWithTitle:@"我是第二个Section" detail:@"8888" type:HerderStyleTotal section:1 canFold:YES];

    } else if (section == 2){

        [headerView setFoldSectionHeaderViewWithTitle:@"我是第三个Section" detail:nil type:HerderStyleNone section:2 canFold:YES];

    } else {

        [headerView setFoldSectionHeaderViewWithTitle:@"我是第四个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;

}

实现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];

}

好了,跳过的坑希望可以帮助更多人不被踩!!!!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,393评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,790评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,391评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,703评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,613评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,003评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,507评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,158评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,300评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,256评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,274评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,984评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,569评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,662评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,899评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,268评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,840评论 2 339

推荐阅读更多精彩内容