美文网首页实用工具iOS学习笔记iOS Developer
UITableView分组的展开与折叠封装

UITableView分组的展开与折叠封装

作者: 见哥哥长高了 | 来源:发表于2017-12-09 13:44 被阅读120次

    好久没写多东西了,唯恐自己被这个世界所遗忘,今天就搞一篇技术文章。(哈哈,刷刷存在感罢了。。。)不废话了,说正经的。之前项目中用到了一个功能:UITableView分组的展开与折叠,今天按其单独模块提出来,分析一下其具体实现逻辑和代码实现:


    功能样式
    实现逻辑

    首先:因为我们操作tableView的时候,控制其展开与折叠的操作是在点击每一个section的头部来完成的,我们就可以从tableView的代理方法:

    -(nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
    

    来入手,因此需要自定义一个响应事件的公共的view。
    其次:自定义一个tableView 继承自:UITableView,实现UITableView的UITableViewDelegate,UITableViewDataSource代理方法,并将这些方法进行一个公共的封装,暴漏成接口,供别的文件来使用。

    实现过程

    下面就来看一下具体代码的实现过程:
    (1)自定义LGJFoldOpenSectionHeader 继承: UIView
    .h

    #import <UIKit/UIKit.h>
    
    //折叠状态
    typedef enum : NSUInteger {
        SectionHeaderStateFold,
        SectionHeaderStateOpen,
    } SectionHeaderState;
    
    //箭头所在位置 (可扩展)
    typedef enum : NSUInteger {
        ArrowPositionLeft,
        ArrowPositionRight,
    } ArrowPosition;
    
    @protocol FoldOpenSectionHeaderDelegate <NSObject>
    
    //点击一个分组的头的以后响应方法
    - (void)foldOpenSectionHeaderTappedAtIndex:(NSInteger)index;
    
    @end
    
    @interface LGJFoldOpenSectionHeader : UIView
    
    @property (nonatomic, weak) id <FoldOpenSectionHeaderDelegate> delegate;
    
    //初始化与属性设置
    - (instancetype)initWithFrame:(CGRect)frame tag:(NSInteger)tag;
    
    - (void)setupWithBackgroundColor:(UIColor *)backgroundColor
                         titleString:(NSString *)titleString
                          titleColor:(UIColor *)titleColor
                           titleFont:(UIFont *)titleFont
                   descriptionString:(NSString *)descriptionString
                    descriptionColor:(UIColor *)descriptionColor
                     descriptionFont:(UIFont *)descriptionFont
                          arrowImage:(UIImage *)arrowImage
                       arrowPosition:(ArrowPosition)arrowPosition
                        sectionState:(SectionHeaderState)sectionState;
    @end
    

    .m

    #import "LGJFoldOpenSectionHeader.h"
    
    //以下几个参数,均可修改,自行设置
    #define LineWidth 0.3f
    #define Margin 8.0f
    #define IconSize 24.0f
    
    @interface LGJFoldOpenSectionHeader()
    
    @property(nonatomic, strong)UILabel *titleLabel;
    @property(nonatomic, strong)UILabel *descriptionLabel;
    @property(nonatomic, strong)UIImageView *arrowImageView;
    @property(nonatomic, strong)CAShapeLayer *sepertorLine;
    @property(nonatomic, assign)SectionHeaderState status;
    @property(nonatomic, assign)ArrowPosition position;
    @property(nonatomic, strong)UITapGestureRecognizer *tap;
    
    @end
    
    @implementation LGJFoldOpenSectionHeader
    
    -(instancetype)initWithFrame:(CGRect)frame tag:(NSInteger)tag{
        self = [super initWithFrame:frame];
        if (self) {
            self.tag = tag;
            [self setupSubviewsWithArrowPosition:ArrowPositionRight];
        }
        return self;
    }
    
    -(void)awakeFromNib{
        [super awakeFromNib];
        [self setupSubviewsWithArrowPosition:ArrowPositionRight];
    }
    
    -(void)setupWithBackgroundColor:(UIColor *)backgroundColor
                        titleString:(NSString *)titleString
                         titleColor:(UIColor *)titleColor
                          titleFont:(UIFont *)titleFont
                  descriptionString:(NSString *)descriptionString
                   descriptionColor:(UIColor *)descriptionColor
                    descriptionFont:(UIFont *)descriptionFont
                         arrowImage:(UIImage *)arrowImage
                      arrowPosition:(ArrowPosition)arrowPosition
                       sectionState:(SectionHeaderState)sectionState{
        [self setBackgroundColor:backgroundColor];
        self.titleLabel.text = titleString;
        self.titleLabel.textColor = titleColor;
        self.titleLabel.font = titleFont;
        
        self.descriptionLabel.text = descriptionString;
        self.descriptionLabel.textColor = descriptionColor;
        self.descriptionLabel.font = descriptionFont;
        self.arrowImageView.image = arrowImage;
        self.status = sectionState;
        self.position = arrowPosition;
        if (sectionState == SectionHeaderStateOpen) {
            if (arrowPosition == ArrowPositionRight) {
                self.arrowImageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
            }else{
                self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI/2);
            }
        }else{
            if (arrowPosition == ArrowPositionRight) {
                _arrowImageView.transform = CGAffineTransformMakeRotation(M_PI/2);
            }else{
                _arrowImageView.transform = CGAffineTransformMakeRotation(0);
            }
        }
    }
    -(void)setupSubviewsWithArrowPosition:(ArrowPosition)arrowPosition{
        CGFloat labelWidth = (self.frame.size.width - Margin * 2 - IconSize) / 2;
        CGFloat labelHeight = self.frame.size.height;
        CGRect arrowRect = CGRectMake(0, (self.frame.size.height - IconSize) / 2, IconSize, IconSize);
        CGRect titleRect =  CGRectMake(Margin + IconSize, 0, labelWidth, labelHeight);
        CGRect descripRect = CGRectMake(Margin + IconSize + labelWidth, 0, labelWidth, labelHeight);
        if (arrowPosition == ArrowPositionRight) {
            arrowRect.origin.x = Margin*2 + labelWidth*2;
            titleRect.origin.x = Margin;
            descripRect.origin.x = Margin + labelWidth;
        }
        [self.titleLabel setFrame:titleRect];
        [self.descriptionLabel setFrame:descripRect];
        [self.arrowImageView setFrame:arrowRect];
        [self.sepertorLine setPath:[self getSepertorPath].CGPath];
        
        [self addSubview:self.titleLabel];
        [self addSubview:self.descriptionLabel];
        [self addSubview:self.arrowImageView];
        [self addGestureRecognizer:self.tap];
        [self.layer addSublayer:self.sepertorLine];
    }
    
    
    -(UIBezierPath *)getSepertorPath{
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:CGPointMake(0, self.frame.size.height - LineWidth)];
        [path addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height - LineWidth)];
        return path;
    }
    
    #pragma mark------Lazy loading
    -(UILabel *)titleLabel{
        if (!_titleLabel) {
            _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
            _titleLabel.backgroundColor = [UIColor clearColor];
            _titleLabel.textAlignment = NSTextAlignmentLeft;
        }
        return _titleLabel;
    }
    -(UILabel *)descriptionLabel{
        if (!_descriptionLabel) {
            _descriptionLabel = [[UILabel alloc]initWithFrame:CGRectZero];
            _descriptionLabel.backgroundColor = [UIColor clearColor];
            _descriptionLabel.textAlignment = NSTextAlignmentRight;
        }
        return _descriptionLabel;
    }
    -(UIImageView *)arrowImageView{
        if (!_arrowImageView) {
            _arrowImageView = [[UIImageView alloc]initWithFrame:CGRectZero];
            _arrowImageView.backgroundColor = [UIColor clearColor];
            _arrowImageView.contentMode = UIViewContentModeScaleAspectFit;
        }
        return _arrowImageView;
    }
    - (CAShapeLayer *)sepertorLine
    {
        if (!_sepertorLine) {
            _sepertorLine = [CAShapeLayer layer];
            _sepertorLine.strokeColor = [UIColor whiteColor].CGColor;
            _sepertorLine.lineWidth = LineWidth;
        }
        return _sepertorLine;
    }
    -(UITapGestureRecognizer *)tap{
        if (!_tap) {
            _tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        }
        return _tap;
    }
    
    -(void)tapAction:(UITapGestureRecognizer *)sender{
        [self shouldExpand:![NSNumber numberWithInteger:self.status].boolValue];
        if ([self.delegate respondsToSelector:@selector(foldOpenSectionHeaderTappedAtIndex:)]) {
            self.status = [NSNumber numberWithBool:(![NSNumber numberWithInteger:self.status].boolValue)].integerValue;
            [self.delegate foldOpenSectionHeaderTappedAtIndex:self.tag];
        }
    }
    
    -(void)shouldExpand:(BOOL)expand{
        [UIView animateWithDuration:.2 animations:^{
            if (expand) {
                if (self.position == ArrowPositionRight) {
                    self.arrowImageView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
                }else{
                    self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
                }
            }else{
                if (self.position == ArrowPositionRight) {
                    self.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI / 2);
                }else{
                    self.arrowImageView.transform = CGAffineTransformMakeRotation(0);
                }
            }
        } completion:^(BOOL finished) {
            if (finished) {
                self.sepertorLine.hidden = expand;
            }
        }];
    }
    @end
    
    具体UI样式如何,我们可以根据不同的应用场景来具体实现,以上只是参考。创建出来之后就是下面这样: 样式1 小图标在右
    样式2 小图标在右

    (2)自定义LGJFoldOpenTableView : UITableView并实现UITableViewDelegate,UITableViewDataSource
    .h

    #import <UIKit/UIKit.h>
    #import "LGJFoldOpenSectionHeader.h"
    @class LGJFoldOpenTableView;
    @protocol LGJFoldOpenTableViewDelegate <NSObject>
    
    @required
    /**
     *  箭头的位置
     */
    - (ArrowPosition)jj_positionForTableView:(LGJFoldOpenTableView *)tableView;
    /**
     *  返回section的个数
     */
    - (NSInteger )jj_numberOfSectionForTableView:(LGJFoldOpenTableView *)tableView;
    /**
     *  cell的个数
     */
    - (NSInteger )jj_foldingTableView:(LGJFoldOpenTableView *)tableView numberOfRowsInSection:(NSInteger )section;
    /**
     *  header的高度
     */
    - (CGFloat )jj_foldingTableView:(LGJFoldOpenTableView *)tableView heightForHeaderInSection:(NSInteger )section;
    /**
     *  cell的高度
     */
    - (CGFloat )jj_foldingTableView:(LGJFoldOpenTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    /**
     *  header的标题
     */
    - (NSString *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView titleForHeaderInSection:(NSInteger )section;
    /**
     *  返回cell
     */
    - (UITableViewCell *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
    /**
     *  点击cell
     */
    - (void )jj_foldingTableView:(LGJFoldOpenTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
    
    
    @optional
    
    /**
     *  箭头图片
     */
    - (UIImage *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView arrowImageForSection:(NSInteger )section;
    
    /**
     *  下面是一些属性的设置
     */
    - (NSString *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView descriptionForHeaderInSection:(NSInteger )section;
    
    - (UIColor *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView backgroundColorForHeaderInSection:(NSInteger )section;
    
    - (UIFont *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView fontForTitleInSection:(NSInteger )section;
    
    - (UIFont *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView fontForDescriptionInSection:(NSInteger )section;
    
    - (UIColor *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView textColorForTitleInSection:(NSInteger )section;
    
    - (UIColor *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView textColorForDescriptionInSection:(NSInteger )section;
    
    @end
    
    @interface LGJFoldOpenTableView : UITableView<UITableViewDelegate,UITableViewDataSource>
    
    @property (nonatomic, weak) id<LGJFoldOpenTableViewDelegate> foldingDelegate;
    @property (nonatomic, assign) SectionHeaderState state;
    
    @end
    

    .m

    #import "LGJFoldOpenTableView.h"
    
    @interface LGJFoldOpenTableView ()<FoldOpenSectionHeaderDelegate>
    
    @property (nonatomic, strong) NSMutableArray *statusArray;
    
    @end
    
    @implementation LGJFoldOpenTableView
    
    #pragma mark------Initialize
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setupDelegateAndDataSource];
        }
        return self;
    }
    
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self) {
            [self setupDelegateAndDataSource];
        }
        return self;
    }
    
    - (void)setupDelegateAndDataSource
    {
        // 适配iOS 11
    #ifdef __IPHONE_11_0
        if ([self respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
            self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        self.estimatedRowHeight = 0;
        self.estimatedSectionHeaderHeight = 0;
        self.estimatedSectionFooterHeight = 0;
    #endif
        
        self.delegate = self;
        self.dataSource = self;
        if (self.style == UITableViewStylePlain) {
            self.tableFooterView = [[UIView alloc] init];
        }
        // 添加监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onChangeStatusBarOrientationNotification:)  name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    }
    
    - (NSMutableArray *)statusArray
    {
        if (!_statusArray) {
            _statusArray = [NSMutableArray array];
        }
        
        if (!_state) {
            _state = SectionHeaderStateFold;
        }
        if (_statusArray.count) {
            if (_statusArray.count > self.numberOfSections) {
                [_statusArray removeObjectsInRange:NSMakeRange(self.numberOfSections - 1, _statusArray.count - self.numberOfSections)];
            }else if (_statusArray.count < self.numberOfSections) {
                for (NSInteger i = self.numberOfSections - _statusArray.count; i < self.numberOfSections; i++) {
                    [_statusArray addObject:[NSNumber numberWithInteger:_state]];
                }
            }
        }else{
            for (NSInteger i = 0; i < self.numberOfSections; i++) {
                [_statusArray addObject:[NSNumber numberWithInteger:_state]];
            }
        }
        return _statusArray;
    }
    
    - (void)onChangeStatusBarOrientationNotification:(NSNotification *)notification
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self reloadData];
        });
    }
    
    #pragma mark - UI Configration
    -(ArrowPosition )perferedArrowPosition{
        if (self.foldingDelegate && [self.foldingDelegate respondsToSelector:@selector(jj_positionForTableView:)]) {
            [self.foldingDelegate jj_positionForTableView:self];
        }
        return ArrowPositionRight;
    }
    - (UIColor *)backgroundColorForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:backgroundColorForHeaderInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self backgroundColorForHeaderInSection:section];
        }
        return [UIColor colorWithRed:102/255.f green:102/255.f blue:255/255.f alpha:1.f];
    }
    
    - (NSString *)titleForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:titleForHeaderInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self titleForHeaderInSection:section];
        }
        return [NSString string];
    }
    
    - (UIFont *)titleFontForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:fontForTitleInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self fontForTitleInSection:section];
        }
        return [UIFont boldSystemFontOfSize:16];
    }
    
    - (UIColor *)titleColorForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:textColorForTitleInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self textColorForTitleInSection:section];
        }
        return [UIColor whiteColor];
    }
    
    - (NSString *)descriptionForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:descriptionForHeaderInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self descriptionForHeaderInSection:section];
        }
        return [NSString string];
    }
    - (UIFont *)descriptionFontForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:fontForDescriptionInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self fontForDescriptionInSection:section];
        }
        return [UIFont boldSystemFontOfSize:13];
    }
    - (UIColor *)descriptionColorForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:textColorForDescriptionInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self textColorForDescriptionInSection:section];
        }
        return [UIColor whiteColor];
    }
    - (UIImage *)arrowImageForSection:(NSInteger )section
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:arrowImageForSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self arrowImageForSection:section];
        }
        return [UIImage imageNamed:@"Arrow"];
    }
    
    #pragma mark - UITableViewDelegate,UITableViewDataSource
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_numberOfSectionForTableView:)]) {
            return [_foldingDelegate jj_numberOfSectionForTableView:self];
        }else{
            return self.numberOfSections;
        }
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (((NSNumber *)self.statusArray[section]).integerValue == SectionHeaderStateOpen) {
            if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:numberOfRowsInSection:)]) {
                return [_foldingDelegate jj_foldingTableView:self numberOfRowsInSection:section];
            }
        }
        return 0;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:heightForHeaderInSection:)]) {
            return [_foldingDelegate jj_foldingTableView:self heightForHeaderInSection:section];
        }else{
            return self.sectionHeaderHeight;
        }
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:heightForRowAtIndexPath:)]) {
            return [_foldingDelegate jj_foldingTableView:self heightForRowAtIndexPath:indexPath];
        }else{
            return self.rowHeight;
        }
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        if (self.style == UITableViewStylePlain) {
            return 0;
        }else{
            return 0.001;
        }
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        LGJFoldOpenSectionHeader *sectionHeaderView = [[LGJFoldOpenSectionHeader alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, [self tableView:self heightForHeaderInSection:section])  tag:section];
        
        [sectionHeaderView setupWithBackgroundColor:[self backgroundColorForSection:section]
                                        titleString:[self titleForSection:section]
                                         titleColor:[self titleColorForSection:section]
                                          titleFont:[self titleFontForSection:section]
                                  descriptionString:[self descriptionForSection:section]
                                   descriptionColor:[self descriptionColorForSection:section]
                                    descriptionFont:[self descriptionFontForSection:section]
                                         arrowImage:[self arrowImageForSection:section]
                                      arrowPosition:[self perferedArrowPosition]
                                       sectionState:((NSNumber *)self.statusArray[section]).integerValue];
        
        sectionHeaderView.delegate = self;
        
        return sectionHeaderView;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:cellForRowAtIndexPath:)]) {
            return [_foldingDelegate jj_foldingTableView:self cellForRowAtIndexPath:indexPath];
        }
        return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DefaultCellIndentifier"];
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (_foldingDelegate && [_foldingDelegate respondsToSelector:@selector(jj_foldingTableView:didSelectRowAtIndexPath:)]) {
            [_foldingDelegate jj_foldingTableView:self didSelectRowAtIndexPath:indexPath];
        }
    }
    #pragma mark - FoldOpenSectionHeaderDelegate
    - (void)foldOpenSectionHeaderTappedAtIndex:(NSInteger)index
    {
        BOOL currentIsOpen = ((NSNumber *)self.statusArray[index]).boolValue;
        
        [self.statusArray replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:!currentIsOpen]];
        
        NSInteger numberOfRow = [_foldingDelegate jj_foldingTableView:self numberOfRowsInSection:!currentIsOpen];
        NSMutableArray *rowArray = [NSMutableArray array];
        if (numberOfRow) {
            for (NSInteger i = 0; i < numberOfRow; i++) {
                [rowArray addObject:[NSIndexPath indexPathForRow:i inSection:index]];
            }
        }
        if (rowArray.count) {
            if (currentIsOpen) {
                [self deleteRowsAtIndexPaths:[NSArray arrayWithArray:rowArray] withRowAnimation:UITableViewRowAnimationTop];
            }else{
                [self insertRowsAtIndexPaths:[NSArray arrayWithArray:rowArray] withRowAnimation:UITableViewRowAnimationTop];
            }
        }
    }
    @end
    
    使用

    准备工作都已经做完了之后,使用起来也是相当的方便,我们需要在使用到的地方导入

    #import "LGJFoldOpenTableView.h"
    

    并实现:LGJFoldOpenTableViewDelegate即可
    如下:

    #import "TestViewController.h"
    
    @interface TestViewController ()<LGJFoldOpenTableViewDelegate>
    
    @property(nonatomic,strong)LGJFoldOpenTableView *tableView;
    @end
    
    @implementation TestViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        self.automaticallyAdjustsScrollViewInsets = NO;
        [self createFoldTableView];
        
    }
    -(void)createFoldTableView{
        LGJFoldOpenTableView *tab = [[LGJFoldOpenTableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
        self.tableView = tab;
        [self.view addSubview:tab];
        tab.foldingDelegate = self;
        if (self.position) {
            tab.state =  SectionHeaderStateOpen;
        }
    }
    
    
    /**
     *  箭头的位置
     */
    - (ArrowPosition)jj_positionForTableView:(LGJFoldOpenTableView *)tableView{
        return self.position ? : ArrowPositionLeft;
    }
    /**
     *  返回section的个数
     */
    - (NSInteger )jj_numberOfSectionForTableView:(LGJFoldOpenTableView *)tableView{
        return 6;
    }
    /**
     *  cell的个数
     */
    - (NSInteger )jj_foldingTableView:(LGJFoldOpenTableView *)tableView numberOfRowsInSection:(NSInteger )section{
        return 5;
    }
    /**
     *  header的高度
     */
    - (CGFloat )jj_foldingTableView:(LGJFoldOpenTableView *)tableView heightForHeaderInSection:(NSInteger )section{
        return 40;
    }
    /**
     *  cell的高度
     */
    - (CGFloat )jj_foldingTableView:(LGJFoldOpenTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return 30;
    }
    /**
     *  header的标题
     */
    - (NSString *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView titleForHeaderInSection:(NSInteger )section{
        return [NSString stringWithFormat:@"第%ld组",section + 1];
    }
    /**
     *  返回cell
     */
    - (UITableViewCell *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *cellID = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
        return cell;
    }
    /**
     *  点击cell
     */
    - (void )jj_foldingTableView:(LGJFoldOpenTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    -(NSString *)jj_foldingTableView:(LGJFoldOpenTableView *)tableView descriptionForHeaderInSection:(NSInteger)section{
        return @"descriptionForHeader";
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
    

    以上也许还有很多不足,欢迎各路大神,批评指正···

    相关文章

      网友评论

        本文标题:UITableView分组的展开与折叠封装

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