基本原理就是利用section的和row的关系,当点击section的头部view时候,就刷新下列表,展开当前section下的row,效果图如下:
- 关键代码
//
// ViewController.m
// TableView的折叠收起
//
// Created by liyang on 16/6/15.
// Copyright © 2016年 liyang. All rights reserved.
//
#import "ViewController.h"
#import "LYTableViewCell.h"
#import "LYHeaderView.h"
#import "LYSectionItem.h"
#import "LYCellItem.h"
static NSString *const cellIdentifier = @"frinedCell";
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, HeaderViewDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@end
@implementation ViewController
- (UITableView *)tableView
{
if (!_tableView) {
UITableView *tableView = [[UITableView alloc] init];
tableView.frame = self.view.bounds;
tableView.dataSource = self;
tableView.delegate = self;
tableView.backgroundColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
tableView.scrollIndicatorInsets = UIEdgeInsetsMake(64, 0, 0, 0);
[self.view addSubview:tableView];
self.tableView = tableView;
}
return _tableView;
}
// 懒加载
- (NSArray *)dataArray
{
if (!_dataArray) {
NSArray *arr = @[ @{
@"sectionName":@"sectionName1",
@"cellItems":@[
@{@"cellItemName":@"cellName1"},
@{@"cellItemName":@"cellName2"},
@{@"cellItemName":@"cellName3"}
]
},
@{
@"sectionName":@"sectionName2",
@"cellItems" :@[
@{@"cellItemName":@"cellName1"},
@{@"cellItemName":@"cellName2"},
@{@"cellItemName":@"cellName3"}
]
},
@{
@"sectionName":@"sectionName3",
@"cellItems":@[
@{@"cellItemName":@"cellName1"},
@{@"cellItemName":@"cellName2"},
@{@"cellItemName":@"cellName3"}
]
}];
NSMutableArray *tempArr = [NSMutableArray array];
for (int i = 0; i < 6; i++) {
for (NSDictionary *dic in arr) {
LYSectionItem *model = [LYSectionItem sectionItemWithDic:dic];
[tempArr addObject:model];
}
}
self.dataArray = tempArr;
[self.tableView reloadData];
}
return _dataArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[LYTableViewCell class] forCellReuseIdentifier:cellIdentifier];
[self dataArray];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
LYSectionItem *sectionItem = self.dataArray[section];
// 条件表达式。得出row行数
return sectionItem.isOpen ? sectionItem.cellItems.count : 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
LYTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
LYSectionItem *sectionItem = self.dataArray[indexPath.section];
LYCellItem *cellItem = sectionItem.cellItems[indexPath.row];
cell.cellItem = cellItem;
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
LYSectionItem *sectionItem = self.dataArray[indexPath.section];
LYCellItem *cellItem = sectionItem.cellItems[indexPath.row];
return cellItem.cellHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
LYSectionItem *sectionItem = self.dataArray[section];
LYHeaderView *header = [LYHeaderView headerView:tableView];
header.delegate = self;
header.sectionItem = sectionItem;
return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
#pragma mark - HeaderViewDelegate
- (void)headerViewClick:(LYHeaderView *)headerView
{
// 改变了isopen的值
[self.tableView reloadData];//刷新列表
}
@end
- 自定义了一个TableView的头视图,当点击它的时候,通过控制模型isOpen属性来达到控制列表是否展开
//
// LYHeaderView.m
// TableView的折叠收起
//
// Created by liyang on 16/6/15.
// Copyright © 2016年 liyang. All rights reserved.
//
#import "LYHeaderView.h"
#import "LYSectionItem.h"
@interface LYHeaderView ()
@property (nonatomic, strong) UIButton *arrowBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation LYHeaderView
+ (instancetype)headerView:(UITableView *)tableView
{
static NSString *kHeadIdentifier = @"header";
LYHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kHeadIdentifier];
if (!headerView) {
headerView = [[self alloc] initWithReuseIdentifier:kHeadIdentifier];
}
return headerView;
}
- (UIButton *)arrowBtn
{
if (!_arrowBtn) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 距离
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
//内容的水平对齐方式
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.imageView.contentMode = UIViewContentModeCenter;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//不裁剪图片
button.imageView.clipsToBounds = NO;
[self addSubview:button];
self.arrowBtn = button;
}
return _arrowBtn;
}
- (UILabel *)titleLabel
{
if (!_titleLabel) {
UILabel *labelRight = [[UILabel alloc] init];
labelRight.textAlignment = NSTextAlignmentCenter;
[self addSubview:labelRight];
self.titleLabel = labelRight;
}
return _titleLabel;
}
- (void)setSectionItem:(LYSectionItem *)sectionItem
{
_sectionItem = sectionItem;
[self.arrowBtn setTitle:sectionItem.name forState:UIControlStateNormal];
self.titleLabel.text = [NSString stringWithFormat:@"%ld", sectionItem.cellItems.count];
// 添加观察者,观察isOpen属性的变化,来调节指示器的状态
[self addObserver:self forKeyPath:@"sectionItem.isOpen" options:NSKeyValueObservingOptionInitial context:nil];
}
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
{
// 改变button图片的旋转读
self.arrowBtn.imageView.transform = self.sectionItem.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}
//布局
- (void)layoutSubviews {
[super layoutSubviews];
self.arrowBtn.frame = self.bounds;
self.titleLabel.frame = CGRectMake(self.bounds.size.width - 70, 0, 60, self.frame.size.height);
}
#pragma mark - buttonAction
- (void)buttonAction:(UIButton *)sender {
//修改groupModel的isOpen属性
self.sectionItem.isOpen = !self.sectionItem.isOpen;
if ([self.delegate respondsToSelector:@selector(headerViewClick:)]) {
[self.delegate headerViewClick:self];
}
}
@end
- 主要就是练习模型控制View
[GitHub地址][1]
[1]:https://github.com/liyang123/TableView-.git