#import "ViewController.h"
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
NSArray *dataArray;//创建一个数据源数组
NSMutableDictionary *dic;//创建一个字典进行判断收缩还是展开
NSArray *sectionArr;//分组的名字
}
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
dic = [NSMutableDictionary dictionary];
dataArray = @[@[@"1"],@[@"1",@"2"],@[@"1",@"2",@"3"],@[@"1",@"2",@"3",@"4"]];
sectionArr = @[@"第一组",@"第二组",@"第三组",@"第四组"];
[self.view addSubview:self.tableView];
}
//懒加载
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREENWIDTH, SCREENHEIGHT - 64) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [[UIView alloc]init];
}
return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
return dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
view.backgroundColor = [UIColor blueColor];
UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
titleLab.text = sectionArr[section];
titleLab.textColor = [UIColor blackColor];
titleLab.userInteractionEnabled = true;
[view addSubview:titleLab];
//创建一个手势进行点击,这里可以换成button
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_tap:)];
view.tag = 300 + section;
[view addGestureRecognizer:tap];
return view;
}
- (void)action_tap:(UIGestureRecognizer *)tap{
NSString *str = [NSString stringWithFormat:@"%ld",tap.view.tag - 300];
if ([dic[str] integerValue] == 0) {//如果是0,就把1赋给字典,打开cell
[dic setObject:@"1" forKey:str];
}else{//反之关闭cell
[dic setObject:@"0" forKey:str];
}
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[str integerValue]]withRowAnimation:UITableViewRowAnimationFade];//有动画的刷新
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *string = [NSString stringWithFormat:@"%ld",section];
if ([dic[string] integerValue] == 1 ) { //打开cell返回数组的count
NSArray *array = [NSArray arrayWithArray:dataArray[section]];
return array.count;
}else{
return 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 35;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
cell.backgroundColor = [UIColor orangeColor];
cell.textLabel.text = dataArray[indexPath.section][indexPath.row];
return cell;
}
@end
iOS tableView列表展开收起
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 使用方法:1、先了解TableViewManager的使用方法。2、建立自己的cell的时候,对应的item继承Z...
- 前言: 最近做商城项目时候,做到tableViewCell 的展开和收缩的时候遇到一个问题,就是当一行在 tabl...
- 一、前言 章节主要是讲述使用UITableView的功能,对多项选择展开收缩作一个封装,从而日后的利用。下图为功能...