iOS 仿QQ折叠 iOS

#import 
//委托者做三件事:
//1.指定协议
@protocol SectionHeaderViewDelegate 
-(void)changedIsAppear;
@end

@interface SectionHeaderView : UIView

@property(nonatomic,strong)UIButton *btn;
@property(nonatomic,assign)BOOL isAppear;//是否展开
//2.声明代理指针
@property(nonatomic,assign)id delegate;


@end


#import "SectionHeaderView.h"

@implementation SectionHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btn.frame = self.bounds;
        [self.btn setBackgroundImage:[UIImage imageNamed:@"btn_on"] forState:UIControlStateNormal];
        [self.btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btn];
    }
    return self;
}
-(void)btnClick{
   
    self.isAppear = !self.isAppear;
    //3.在合适的时机调用协议方法
    [self.delegate changedIsAppear];

}


@end


#import "RootViewController.h"
#import "FriendModelManager.h"
#import "SectionHeaderView.h"

@interface RootViewController ()
{
    NSMutableArray *_dataArray;//数据源
    UITableView *_myTableView;
    NSMutableArray *_headViewArray;//保存所有的头部视图
}
@end

@implementation RootViewController

-(void)requestData{
   
    _dataArray = [[FriendModelManager allFriendsModel] mutableCopy];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //请求数据
    [self requestData];
    //创建tableView
    [self creatTableView];
    //创建section的头部视图
    [self creatSectionHeadView];
   
}

-(void)creatSectionHeadView{
    _headViewArray = [[NSMutableArray alloc] init];
   
    //循环创建5个头部视图,把5个视图依此加入到一个数组里
    for (int i = 0; i<_dataArray.count; i++) {
        SectionHeaderView *headView = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        [headView.btn setTitle:[NSString stringWithFormat:@"%d组",i+1] forState:UIControlStateNormal];
        //指定代理对象
        headView.delegate = self;
        [_headViewArray  addObject:headView];
    }

}

-(void)creatTableView{
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
   
    //注册Cell
    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellName"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_dataArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SectionHeaderView *view = _headViewArray[section];
    return view.isAppear?[_dataArray[section] count]:0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:@"CellName" forIndexPath:indexPath];
   
    FriendModel *model = _dataArray[indexPath.section][indexPath.row];
    cell.imageView.image = [UIImage imageNamed:model.icon];
    cell.textLabel.text = model.name;
   
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return _headViewArray[section];
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

#pragma mark - SectionHeaderViewDelegate

-(void)changedIsAppear
{
    //刷新tableView
    [_myTableView  reloadData];

}

@end

你可能感兴趣的:(iOS 仿QQ折叠 iOS)