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镊靴!