仿QQ联系人的TableView的折叠与拉伸

最近手上任务比较轻,打算把一些之前做一些的简单的东西再整理整理,优化一下,做个记录; 

TableView的折叠拉伸主要是在一些联系人分组里会经常见到,今天做了一个简单的demo,代码也做了处理,随拿随用。

思路整理:

1.根据数据设置分组

2.设置折叠/拉伸状态标识

3.实现分组透视图点击事件

实现代码:

#import "FoldTableViewController.h"

#define STATE @"state"

#define INFO @"info"

@interface FoldTableViewController ()

{

NSMutableArray *_dataArray;//数据源数组

}

@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];

}

//创建数据

- (void)requestData{

/**

假设有i组数据,每组有j条内容:

每组实例化一个可变字典存储组内数据,STATE对应当前状态(折叠/拉伸),INFO对应当前内容;

*/

_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];

}

//取到当前cell的内容

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

/**在刷新视图时,根据当前分组的状态,调整头视图的内容视图

*/

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

/**点击头视图,改变该组状态

*/

if ([dict[STATE] isEqualToString:@"1"]) {

[dict setValue:@"0" forKey:STATE];

}else{

[dict setValue:@"1" forKey:STATE];

}

/**刷新当前点击分组的数据

reloadSections:需要刷新的分组

withRowAnimation:刷新的动画方式

*/

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationNone];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 课程要点:plist文件的新建与读取给UITableView设置变化的值单元格的删除、插入及刷新 plist...
    shiwuoo阅读 2,661评论 0 6
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,093评论 3 38
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改变导航栏背景颜...
    SadMine阅读 1,650评论 1 4
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,453评论 9 5
  • 车子驶过广场,总觉得有某些不对?哦!前段时间还在雨水中泥泞,在阳光下灰飞尘扬的人行道已被水泥粉饰一平。青...
    清草阅读 289评论 0 1