仿QQ联系人的TableView的折叠与拉伸
最近手上任務(wù)比較輕,打算把一些之前做一些的簡單的東西再整理整理,優(yōu)化一下,做個記錄;?
TableView的折疊拉伸主要是在一些聯(lián)系人分組里會經(jīng)常見到,今天做了一個簡單的demo,代碼也做了處理,隨拿隨用。
思路整理:
1.根據(jù)數(shù)據(jù)設(shè)置分組
2.設(shè)置折疊/拉伸狀態(tài)標(biāo)識
3.實現(xiàn)分組透視圖點擊事件
實現(xiàn)代碼:
#import "FoldTableViewController.h"
#define STATE @"state"
#define INFO @"info"
@interface FoldTableViewController ()
{
NSMutableArray *_dataArray;//數(shù)據(jù)源數(shù)組
}
@end
@implementation FoldTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setExtraCellLineHidden];
[self requestData];
}
//底部視圖留白
- (void)setExtraCellLineHidden{
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:view];
}
//創(chuàng)建數(shù)據(jù)
- (void)requestData{
/**
假設(shè)有i組數(shù)據(jù),每組有j條內(nèi)容:
每組實例化一個可變字典存儲組內(nèi)數(shù)據(jù),STATE對應(yīng)當(dāng)前狀態(tài)(折疊/拉伸),INFO對應(yīng)當(dāng)前內(nèi)容;
*/
_dataArray = [[NSMutableArray alloc]init];
for (int i = 0; i < 5; i++) {
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
NSMutableArray *tempArr = [[NSMutableArray alloc]init];
for (int j = 0; j < 10; j++) {
NSString *infoStr = [NSString stringWithFormat:@"test%d",j];
[tempArr addObject:infoStr];
}
[tempDict setValue:@"1" forKey:STATE];
[tempDict setValue:tempArr forKey:INFO];
[_dataArray addObject:tempDict];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *state = _dataArray[section][STATE];
if ([state isEqualToString:@"0"]) {
return 0;
}
return [_dataArray[section][@"info"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ide = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ide];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ide];
}
//取到當(dāng)前cell的內(nèi)容
cell.textLabel.text = _dataArray[indexPath.section][@"info"][indexPath.row];
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 20)];
headView.tag = 10+section;
headView.backgroundColor = [UIColor grayColor];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 20, 20)];
[headView addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 5, 100, 20)];
titleLabel.text = [NSString stringWithFormat:@"section%ld",section];
titleLabel.textColor = [UIColor whiteColor];
[headView addSubview:titleLabel];
/**在刷新視圖時,根據(jù)當(dāng)前分組的狀態(tài),調(diào)整頭視圖的內(nèi)容視圖
*/
NSDictionary *dict = _dataArray[section];
if ([dict[STATE] isEqualToString:@"1"]) {
imageView.image = [UIImage imageNamed:@"arrow_spread"];
}else{
imageView.image = [UIImage imageNamed:@"arrow_fold"];
}
/**為頭視圖添加輕觸事件
*/
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headViewClick:)];
[headView addGestureRecognizer:tap];
return headView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (void)headViewClick:(UITapGestureRecognizer *)tap{
NSInteger index = tap.view.tag-10;
NSMutableDictionary *dict = _dataArray[index];
/**點擊頭視圖,改變該組狀態(tài)
*/
if ([dict[STATE] isEqualToString:@"1"]) {
[dict setValue:@"0" forKey:STATE];
}else{
[dict setValue:@"1" forKey:STATE];
}
/**刷新當(dāng)前點擊分組的數(shù)據(jù)
reloadSections:需要刷新的分組
withRowAnimation:刷新的動畫方式
*/
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
轉(zhuǎn)載于:https://juejin.im/post/5a31d4d451882526151a9ac2
總結(jié)
以上是生活随笔為你收集整理的仿QQ联系人的TableView的折叠与拉伸的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过Xcode断点集成 reveal(2
- 下一篇: 这个时代,给了我们年轻人太多