首先自定义一个继承UITableViewHeaderFooterView的类:
因为在点击头部的时候要判断它是第几个section,在此使用协议来实现
.h文件:
@class AddStaffHeadView;
@protocol StaffListDisplay <NSObject>
- (void)headViewOpenStatusChanged:(AddStaffHeadView *)headView;//使用协议来判断哪个头部,是否点击了折叠展开按钮,所以最好传参数Headview
@end
@interface AddStaffHeadView : UITableViewHeaderFooterView
@property(nonatomic , strong) UIView *titleView;
@property(nonatomic , strong) UILabel *deptNameLabel;
@property(nonatomic , strong) UIView *seperateLineView;
@property(nonatomic , strong) UIView *addView;
@property(nonatomic , strong) UIImageView *addImageView;
@property(nonatomic , strong) UILabel *addTitleLabel;
@property(nonatomic , strong) UIButton *foldBtn;
@property(nonatomic , assign) NSInteger section;
@property(nonatomic , assign) BOOL opened;
@property(nonatomic , assign) id<StaffListDisplay> displayDelegate;
@end
.m文件:
#import "AddStaffHeadView.h"
@implementation AddStaffHeadView
- (instancetype) initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
[self addViews];
[self makeViewsConstraint];
}
return self;
}
- (void) addViews
{
[self addSubview:self.titleView];
[_titleView addSubview:self.deptNameLabel];
[self addSubview:self.seperateLineView];
[self addSubview:self.addView];
[_addView addSubview:self.addImageView];
[_addView addSubview:self.addTitleLabel];
[_titleView addSubview:self.foldBtn];
}
- (void) makeViewsConstraint
{
[_addView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self);
make.bottom.equalTo(self);
make.height.equalTo(@30);
}];
[_seperateLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self);
make.height.equalTo(@0.5);
make.bottom.equalTo(_addView.mas_top);
}];
[_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.equalTo(self);
make.top.equalTo(self);
make.bottom.equalTo(_seperateLineView.mas_top);
}];
[_deptNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@15);
make.right.equalTo(self).offset(-150);
make.centerY.equalTo(_titleView);
}];
[_foldBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(@(0));
make.centerY.equalTo(_titleView);
make.left.equalTo(_deptNameLabel.mas_right).offset(10);
// make.width.equalTo(@10);
}];
[_addImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(@15);
make.centerY.equalTo(_addView);
make.width.equalTo(@20);
make.height.equalTo(@20);
}];
[_addTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_addImageView.mas_right).offset(10);
make.centerY.equalTo(_addView);
make.width.equalTo(@150);
}];
}
#pragma mark getter
- (UIView *) titleView
{
if (!_titleView) {
_titleView = [[UIView alloc] init];
_titleView.backgroundColor = [UIColor whiteColor];
}
return _titleView;
}
- (UILabel *) deptNameLabel
{
if (!_deptNameLabel) {
_deptNameLabel = [[UILabel alloc] init];
_deptNameLabel.font = MC_12_Font;
}
return _deptNameLabel;
}
- (UIView *) seperateLineView
{
if (!_seperateLineView) {
_seperateLineView = [[UIView alloc] init];
_seperateLineView.backgroundColor = [UIColor lightGrayColor];
}
return _seperateLineView;
}
- (UIView *) addView
{
if (!_addView) {
_addView = [[UIView alloc] init];
_addView.backgroundColor = [UIColor whiteColor];
}
return _addView;
}
- (UIImageView *) addImageView
{
if (!_addImageView) {
_addImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"addImage"]];
}
return _addImageView;
}
- (UILabel *) addTitleLabel
{
if (!_addTitleLabel) {
_addTitleLabel = [[UILabel alloc] init];
_addTitleLabel.text = @"添加员工";
_addTitleLabel.font = MC_16_Font;
_addTitleLabel.textColor = [UIColor grayColor];
}
return _addTitleLabel;
}
- (UIButton *) foldBtn
{
if (!_foldBtn) {
_foldBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_foldBtn setImage:[UIImage imageNamed:@"fold_arrow"] forState:UIControlStateNormal];
[_foldBtn setImage:[UIImage imageNamed:@"unfold_arrow"] forState:UIControlStateSelected];
[_foldBtn bk_addEventHandler:^(id sender) {
if (self.opened) {
self.opened = NO;
} else {
self.opened = YES;
}
if ([self.displayDelegate respondsToSelector:@selector(headViewOpenStatusChanged:)]) {
[self.displayDelegate headViewOpenStatusChanged:self];//调用协议
}
} forControlEvents:UIControlEventTouchUpInside];
}
return _foldBtn;
}
- (void)setOpened:(BOOL)opened
{
_opened = opened;
self.foldBtn.selected = opened;
}
@end
在要使用的tableview写上协议:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
AddStaffHeadView *deptView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"WorkHeadView"];
QueryDeptInfoListModel *deptInfo = self.deptInfoModel.List[section];
deptView.deptNameLabel.text = [NSString stringWithFormat:@"%@ (%@/%@)",deptInfo.DeptName,deptInfo.StaffNum,deptInfo.StaffNum];
//添加员工加单击手势
UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addWorkerClicked:)];
[deptView.addView addGestureRecognizer:tapGesture];
tapGesture.view.tag = section;//巧妙使用手势的tag属性
NSNumber *opened = self.openlist[section];
deptView.opened = opened.boolValue;
deptView.section = section;
deptView.displayDelegate = self;
return deptView;
}
- (void) addWorkerClicked :(UITapGestureRecognizer *) recognizer
{
NSInteger section = recognizer.view.tag;
QueryDeptInfoListModel *deptInfoListModel= [QueryDeptInfoListModel mj_objectWithKeyValues:_deptInfoModel.List[section]];
MCAddWorkerViewController *addWorkerVC = [[MCAddWorkerViewController alloc] init];
addWorkerVC.deptId = deptInfoListModel.DeptId;
[self.navigationController pushViewController:addWorkerVC animated:YES];
}
协议的实现:
- (void)headViewOpenStatusChanged:(AddStaffHeadView *)headView
{
self.openlist[headView.section]=@(headView.opened);
NSNumber *open = self.openlist[headView.section];
if (open.boolValue == YES) {
QueryDeptInfoListModel *deptInfoList = [QueryDeptInfoListModel mj_objectWithKeyValues:_deptInfoModel.List[headView.section]];
[self requestNetwork:deptInfoList.DeptId section:headView.section];
}
else
{
[_tableView reloadSections:[NSIndexSet indexSetWithIndex:headView.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
在最开始先默认折叠状态
_openlist = [NSMutableArray array];
for (int i = 0; i < self.deptInfoModel.List.count; i++) {
[_openlist addObject:@NO];
}
效果图: