美文网首页
iOS-Section_Cell的折叠与展开

iOS-Section_Cell的折叠与展开

作者: Simple_Code | 来源:发表于2018-05-29 15:44 被阅读269次
#import <UIKit/UIKit.h>

@interface SectionFoldViewController : UIViewController

@end


#import "SectionFoldViewController.h"

static const CGFloat SectionHeight = 44.f;
typedef NS_ENUM(NSInteger,SectionFoldType) {
    SectionIsFold = 0, //折叠
    SectionIsUnFold    //展开
};

@interface SectionFoldViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *sectionArray;
@property (nonatomic,strong)NSMutableArray *flagArray;
@end

@implementation SectionFoldViewController

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

#pragma mark - 页面加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self sp_layoutNavigation];
    [self sp_getNewData];
    [self sp_addSubviews];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置navigationItem
- (void)sp_layoutNavigation {
    self.navigationItem.title = @"Section折叠与展开";
}

#pragma mark - 创建tableView
- (void)sp_addSubviews {
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
}

#pragma mark - 加载数据
- (void)sp_getNewData {
    _sectionArray = [NSMutableArray array];
    _flagArray  = [NSMutableArray array];
    NSInteger num = 10;
    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:@(SectionIsFold)];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _sectionArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arr = _sectionArray[section];
    return arr.count;}
- (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:@"第%ld组的第%ld个cell",indexPath.section,indexPath.row];
    cell.clipsToBounds = YES; //这句话很重要 不信你就试试
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [_flagArray[indexPath.section] boolValue] == SectionIsFold ? 0 : SectionHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return SectionHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1.f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *sectionLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor whiteColor];
        label.frame = CGRectMake(0, 0, self.view.frame.size.width, SectionHeight);
        label.textColor = [UIColor redColor];
        label.text = [NSString stringWithFormat:@"组%ld",section];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 100 + section;
        label.userInteractionEnabled = YES;
        label;
    });
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionClick:)];
    [sectionLabel addGestureRecognizer:tap];
    
    return sectionLabel;
}
- (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];
    }
    
    // 展开
    // 方法一 通过刷新Rows的方法刷新
    /*
     if ([_flagArray[index] boolValue] == SectionIsFold) {
     _flagArray[index] = @(SectionIsUnFold);
     [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationBottom];  //使用下面注释的方法就 注释掉这一句
     } else { //收起
     _flagArray[index] = @(SectionIsFold);
     [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop]; //使用下面注释的方法就 注释掉这一句
     }
     */
    
    // 方法一简写
    _flagArray[index] = @(![_flagArray[index] boolValue]);
    UITableViewRowAnimation type = [_flagArray[index] boolValue] ? UITableViewRowAnimationBottom : UITableViewRowAnimationTop;
    [_tableView reloadRowsAtIndexPaths:indexArray withRowAnimation: type];
    
    
    /*
     // 方法二 通过刷新Sections的方法刷新
     _flagArray[index] = @(![_flagArray[index] boolValue]);
     NSRange range = NSMakeRange(index, 1);
     NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
     [_tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationAutomatic];
     */
}

@end

大致思路

_flagArray 存放控制Section展开与折叠的Bool值
_sectionArray存放cell的数据
通过改变_flagArray存放的Bool值控制Section展开与折叠
(也可以使用model进行控制、感觉比较简单)

另外

上面代码可直接赋值粘贴测试

单个Cell的展开与折叠可通过直接改变cell的高度进行折叠控制

代码下载(里面的Section的展开与折叠)

相关文章

  • iOS-Section_Cell的折叠与展开

    大致思路 _flagArray 存放控制Section展开与折叠的Bool值_sectionArray存放cell...

  • 展开与折叠

    经过动手操作,我发现如下情况: 一、附页图1中长方体的1和6面相对,2和4面相对,3和5面相对;正方体的1和5...

  • 展开与折叠

  • 展开与折叠

    今天,我们用数学书上给的展开图,做了长方体和正方体,有些展开雨可以做成长方体或正方体,有些则不行。 那是因为有些的...

  • 展开与折叠

    符合折叠条件的已经折叠完成。但还有四个展开图,由于不符合折叠条件而拼不成长方体和正方体。 其中有两个展开图不符...

  • 折叠展开

    早上阅读了最近很火的《北京折叠》,文章不长,很快读完,中间有很多的描写还是跳过的,因为不是精读,因此很多细节并不清...

  • spacemacs常用操作

    折叠 zc 折叠当前块(函数)zm 折叠当前文件的所有函数zo 展开当前折叠zr 展开当前文件的所有折叠 函数跳转...

  • Flutter代码的一些快捷键

    折叠部分代码: ⌘ + -展开部分代码: ⌘ + +折叠全部代码: ⌘ + ⇧ + -展开全部代码: ⌘ + ⇧ ...

  • TableView折叠(展开与收起)

    前言:tableView的折叠效果在很多场景上都会使用,类似QQ联系人;下面我们通过对数据模型灵活运用来实现这一功...

  • 《展开与折叠》教学反思

    《展开与折叠》这一节课的主要目标是结合具体的长方体和正方体的展开与折叠情景,掌握长方体和正方体六个面相对位置的...

网友评论

      本文标题:iOS-Section_Cell的折叠与展开

      本文链接:https://www.haomeiwen.com/subject/evlajftx.html