iOS多級展開列表的實現(xiàn)

iOS多級展開列表的實現(xiàn)

效果圖

multTableView.gif

用法(類似UITableView)

初始化XDMultTableView

#import "XDMultTableView.h"
...
@property(nonatomic, readwrite, strong)XDMultTableView *tableView;

 _tableView = [[XDMultTableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
    _tableView.openSectionArray = [NSArray arrayWithObjects:@1,@2, nil];
    _tableView.delegate = self;
    _tableView.datasource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableView];

實現(xiàn)數(shù)據(jù)源


- (NSInteger)mTableView:(XDMultTableView *)mTableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (XDMultTableViewCell *)mTableView:(XDMultTableView *)mTableView
              cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [mTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    UIView *view = [[UIView alloc] initWithFrame:cell.bounds] ;
    view.layer.backgroundColor  = [UIColor whiteColor].CGColor;
    view.layer.masksToBounds    = YES;
    view.layer.borderWidth      = 0.3;
    view.layer.borderColor      = [UIColor lightGrayColor].CGColor;
    
    cell.backgroundView = view;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(XDMultTableView *)mTableView{
    return 6;
}

-(NSString *)mTableView:(XDMultTableView *)mTableView titleForHeaderInSection:(NSInteger)section{
    return @"我是頭部";
}

實現(xiàn)代理


- (CGFloat)mTableView:(XDMultTableView *)mTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)mTableView:(XDMultTableView *)mTableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (void)mTableView:(XDMultTableView *)mTableView willOpenHeaderAtSection:(NSInteger)section{
    NSLog(@"即將展開");
}

- (void)mTableView:(XDMultTableView *)mTableView willCloseHeaderAtSection:(NSInteger)section{
    NSLog(@"即將關(guān)閉");
}

- (void)mTableView:(XDMultTableView *)mTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"點擊cell");
}

列表展開關(guān)閉的實現(xiàn)原理

在section header注冊一個手勢

//section header view 設(shè)置tag值為section
view.tag = section;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHeader:)];
    [view addGestureRecognizer:tap];
  

手勢的響應(yīng)事件


- (void)tapHeader:(UITapGestureRecognizer *) tap {
    NSInteger section = tap.view.tag;
    NSNumber *sectionObj = [NSNumber numberWithInteger:section];
    UIImageView *imageView = (UIImageView *)[tap.view viewWithTag:100];
    //_multopenSectionArray 用于記錄每個 section的展開和關(guān)閉狀態(tài)
    if ([_multopenSectionArray containsObject:sectionObj]) {
        NSArray *deleteArray = [self buildDeleteRowWithSection:section];
        [_multopenSectionArray removeObject:sectionObj];
        //想關(guān)閉的section的所有indexPath
        [_tableView deleteRowsAtIndexPaths:deleteArray withRowAnimation:UITableViewRowAnimationFade];
        [UIView animateWithDuration:0.3 animations:^{
            imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);
        }];
    }else{
        [_multopenSectionArray addObject:sectionObj];
        //想展開的section的所有indexPath
        NSArray *insertArray = [self buildInsertRowWithSection:section];
        [_tableView insertRowsAtIndexPaths:insertArray withRowAnimation:UITableViewRowAnimationFade];
        [UIView animateWithDuration:0.3 animations:^{
            imageView.transform = CGAffineTransformMakeRotation(0);
        }];
    }
}

源碼地址:https://github.com/caixindong/XDMultTableView

想了解更多iOS相關(guān)知識,歡迎訂閱我的小專欄

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容