天天看點

實作類似QQ的折疊效果

//  主要核心是點選自定義header來展開和收起每一組裡面的cell,模型裡面應該有isShow此屬性來記錄開展還是收起。

//  ViewController.m

//  實作類似QQ的折疊效果

//

//  Created by skg_Myfly on 16/3/3.

//  Copyright © 2016年 skg_Myfly. All rights reserved.

//

#import "ViewController.h"

#import "QQCellModel.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

/** tableview */

@property (nonatomic , strong ) UITableView *tableView;

/** 全局資料源 */

@property (nonatomic , strong ) NSMutableArray *dateArray;

@end

@implementation ViewController

//懶加載

-(NSMutableArray *)dateArray{

    if (!_dateArray) {

        self.dateArray = [NSMutableArray array];

    }

    return _dateArray;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    //添加資料

    for (int i = \'A\';  i <= \'Z\'; i ++) {

        QQCellModel* model =   [[QQCellModel alloc] init];

        model.name =  [NSString stringWithFormat:@"%c",i];

//        model.isShow = YES;

        for (int j = 0;  j < 10; j ++) {

            [model.array addObject: [NSString stringWithFormat:@"%c - %d",i,j]];

        }

        [self.dateArray addObject:model];

    }

//執行個體化表單

    _tableView =  [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    _tableView.rowHeight = 50;

    [self.view addSubview:_tableView];

}

//合計多少組

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return  [_dateArray count];

}

//每組裡面含多少行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    QQCellModel* model =  [_dateArray objectAtIndex:section];

    if (model.isShow) {

        return  model.array.count;

    }else{

        return 0;

    }

}

//添加每行顯示的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString* ID = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID ];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

    }

    QQCellModel* model =  [_dateArray objectAtIndex:indexPath.section];

    NSString* str =  [[model array] objectAtIndex:indexPath.row];

    cell.textLabel.text = str;

    return cell;

}

// 定義頭标題的視圖,添加點選事件

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    QQCellModel* model =  [_dateArray objectAtIndex:section];

    UIButton* btn =[UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50);

    [btn setTitle:model.name forState:UIControlStateNormal];

    btn.tag = section;

    [btn addTarget:self action:@selector(btnClick:with:) forControlEvents:UIControlEventTouchUpInside];

    if (section % 2) {

        btn.backgroundColor =  [UIColor darkGrayColor];

    }else{

        btn.backgroundColor =  [UIColor lightGrayColor];

    }

    return btn;

}

//點選後事件相應

-(void)btnClick:(UIButton*)btn with:(NSInteger*)section{

    QQCellModel* model =  [_dateArray objectAtIndex:btn.tag];

    if ([model isShow]) {

        [model setIsShow:NO];

    }else{

        [model setIsShow:YES];

    }

    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:btn.tag] withRowAnimation:UITableViewRowAnimationFade];

//    [_tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]

}

@end

-------------------------------------------------------------------------------------------

//

//  QQCellModel.h

//  實作類似QQ的折疊效果

//

//  Created by skg_Myfly on 16/3/3.

//  Copyright © 2016年 skg_Myfly. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface QQCellModel : NSObject

/**名字 */

@property (nonatomic ,copy ) NSString* name;

/**是否展開 */

@property (nonatomic , assign ) BOOL isShow;

/** 每組的資料 */

@property (nonatomic , strong ) NSMutableArray *array;

@end

實作類似QQ的折疊效果