iOS开发,UITableView中section的展开和收起
2017-03-09 本文已影响1969人
KennyHito
个人链接
- 博客园主页 : 博客园主页
- GitHub : 我的GitHub
- iOS程序猿(媛)~~ : 这是我个人整理的一个技术专题, 这里的文章都是比较有技术含量(不断更新)!
- 微信公众号 :

功能部分:</br>
一.思路:在写代码的时候我们可以很容易的写出cell和setion。但是系统并没有提供记录section状态的方法或是属性。我们需要点击某个section的时候收起和弹出cell。怎么做呢?只有是人为的给section增加一个标记了,每个section一个标记,section被点击了就把这个状态标记取反,根据这个标记来展开和收起cell(其实就是设置cell的高度,高度为0了cell就收起了,高度大于0了cell就弹出了,在具体设置cell的高度的时候也是这么处理的)。
二.下面就直接贴代码了。
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *sectionArray;
@property (nonatomic,strong)NSMutableArray *flagArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self makeData];
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 处理数据 _sectionArray里面存储数组
*/
- (void)makeData{
_sectionArray = [NSMutableArray array];
_flagArray = [NSMutableArray array];
NSInteger num = 6;
for (int i = 0; i < num; i ++) {
NSMutableArray *rowArray = [NSMutableArray array];
for (int j = 0; j < arc4random()%20 + 1; j ++) {
[rowArray addObject:[NSString stringWithFormat:@"%d",j]];
}
[_sectionArray addObject:rowArray];
[_flagArray addObject:@"0"];
}
}
//设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _sectionArray.count;
}
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *arr = _sectionArray[section];
return arr.count;
}
//组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 44;
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([_flagArray[indexPath.section] isEqualToString:@"0"])
return 0;
else
return 44;
}
//组头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *sectionLabel = [[UILabel alloc] init];
sectionLabel.frame = CGRectMake(0, 0, self.view.frame.size.width, 444);
sectionLabel.textColor = [UIColor orangeColor];
sectionLabel.text = [NSString stringWithFormat:@"组%d",section];
sectionLabel.textAlignment = NSTextAlignmentCenter;
sectionLabel.tag = 100 + section;
sectionLabel.userInteractionEnabled = YES;
sectionLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"itembg.png"]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionClick:)];
[sectionLabel addGestureRecognizer:tap];
return sectionLabel;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identify = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];
}
cell.textLabel.text= [NSString stringWithFormat:@"第%d组的第%d个cell",indexPath.section,indexPath.row];
cell.clipsToBounds = YES;<span style="font-size:18px;color:#ff0000;"><strong>//这句话很重要 不信你就试试</strong></span>
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SectionViewController *sVC = [[SectionViewController alloc] init];
sVC.rowLabelText = [NSString stringWithFormat:@"第%d组的第%d个cell",indexPath.section,indexPath.row];
[self presentViewController:sVC animated:YES completion:nil];
}
- (void)sectionClick:(UITapGestureRecognizer *)tap{
int index = tap.view.tag % 100;
NSMutableArray *indexArray = [[NSMutableArray alloc]init];
NSArray *arr = _sectionArray[index];
for (int i = 0; i < arr.count; i ++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:index];
[indexArray addObject:path];
}
//展开
if ([_flagArray[index] isEqualToString:@"0"]) {
_flagArray[index] = @"1";
[_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationBottom]; //使用下面注释的方法就 注释掉这一句
} else { //收起
_flagArray[index] = @"0";
[_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop]; //使用下面注释的方法就 注释掉这一句
}
// NSRange range = NSMakeRange(index, 1);
// NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
// [_tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
三.代码下载:
https://github.com/NSLog-YuHaitao/UITableView-section
声明
- 所有文章出自 Kenny Hito 的博客 !
- 未经本人允许不得转载, 转载请标明来源与作者, 谢谢合作!