可折疊展開的tableView,QQ好友分組列表
demo下載地址https://github.com/zhengwenming/ExpandTableView
原理分析:這個(gè)可以折疊的table,我們點(diǎn)擊的是table的section,每個(gè)section下面都對(duì)應(yīng)一個(gè)數(shù)組,點(diǎn)擊section,就展開sction然后展示數(shù)組數(shù)據(jù)。每次點(diǎn)擊section都要刷新當(dāng)前點(diǎn)擊的這個(gè)section,不用reloadData,提高效率。那么點(diǎn)擊的這個(gè)sction怎么知道自己是展開呢還是折疊起來呢?那么關(guān)鍵就是對(duì)這里的處理,我們自己要加一個(gè)條件判斷是展開還是折疊。下面上代碼看看具體實(shí)現(xiàn)。
方法一的原理是用一個(gè)stateArray去記錄每個(gè)section的狀態(tài),當(dāng)然光記錄還是不行的,還是不斷的改變這個(gè)stateArray對(duì)應(yīng)section的值,展開了就把值替換為1,閉合了替換了0.那么這個(gè)0和1就是我們的依據(jù),依據(jù)這個(gè)就可以返回這個(gè)scetion對(duì)應(yīng)的row了。
給section中的按鈕添加點(diǎn)擊事件,然后設(shè)置按鈕的tag值為section。
方法二中用一個(gè)類去記錄和不斷的替換狀態(tài),用一個(gè)model類。看看這個(gè)model類的定義
再看看怎么用
(UIView?)tableView:(UITableView?)tableView viewForHeaderInSection:(NSInteger)section?
{?
UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];?
sectionView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];?
GroupModel *groupModel = dataSource[section];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];?
[button setFrame:sectionView.bounds];?
[button setTag:section];?
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];?
[button setTitle:groupModel.groupName forState:UIControlStateNormal];?
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];?
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];?
[sectionView addSubview:button];?
UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];?
[line setImage:[UIImage imageNamed:@”line_real”]];?
[sectionView addSubview:line];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-16)/2, 14, 16)];?
[imgView setImage:[UIImage imageNamed:@”ico_list”]];?
[sectionView addSubview:imgView];
UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-40, (44-20)/2, 40, 20)];?
[numberLabel setBackgroundColor:[UIColor clearColor]];?
[numberLabel setFont:[UIFont systemFontOfSize:14]];?
NSInteger onLineCount = 0;?
for (NSDictionary *friendInfoDic in groupModel.groupFriends) {?
if ([friendInfoDic[@”status”] isEqualToString:@”1”]) {?
onLineCount++;?
}?
}?
[numberLabel setText:[NSString stringWithFormat:@”%ld/%ld”,onLineCount,groupModel.groupCount]];?
[sectionView addSubview:numberLabel];
return sectionView;?
}?
- (void)tableView:(UITableView?)tableView didSelectRowAtIndexPath:(NSIndexPath?)indexPath?
{?
GroupModel *groupModel = dataSource[indexPath.section];?
NSDictionary *friendInfoDic = groupModel.groupFriends[indexPath.row];?
NSLog(@”%@ %@”,friendInfoDic[@”name”],friendInfoDic[@”shuoshuo”]);?
}
(void)buttonPress:(UIButton *)sender//headButton點(diǎn)擊?
{?
GroupModel *groupModel = dataSource[sender.tag];?
groupModel.isOpened = !groupModel.isOpened;?
[expandTable reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];?
}
看看關(guān)鍵代碼,就是按鈕點(diǎn)擊事件里面buttonPress,這一句話?
groupModel.isOpened = !groupModel.isOpened;?
的意思是如果是展開,那么點(diǎn)擊之后就是折疊,?
如果是折疊,點(diǎn)擊之后就是展開。這個(gè)狀態(tài)被記錄下來了,而且可以不斷的改變。
好,到此為止。兩種方法介紹完畢。想看demo的同學(xué)可以直接到Github上下載https://github.com/zhengwenming/ExpandTableView