VC
//返回每一组需要显示的头部标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
}
// 1.创建头部控件
XSHeaderView *header = [XSHeaderView headerViewWithTableView:tableView];
header.delegate = self;
header.stagesModel = self.model.stages[section-1];
header.section = section-1;
return header;
}
//MARK: - CustomDelegate 代理:点击header
- (void)headerViewDidClickedNameView:(XSHeaderView *)headerView
{
self.model.stages[headerView.section].isOpen = !self.model.stages[headerView.section].isOpen;
//1、请求数据
XSScheduleDetialModel* detailModel = [_flagDic objectForKey:[NSString stringWithFormat:@"section%ld",headerView.section]];
if (!detailModel) {
[self showLoadingHud];
[self loadScheduleDataWithSection:headerView.section];
}else{
if (detailModel.stages.firstObject.periods.count == 0) {
[XSPopoverView showPopoverWithMessage:@"暂无课程安排"];
return ;
}else{
[self.tableView reloadData];
}
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == 0) {
return 0;
}else{
return 75;
}
}
View
#import <UIKit/UIKit.h>
#import "XSHomeScheduleModel.h"
@class XSHeaderView;
@protocol XSHeaderViewDelegate <NSObject>
@optional
- (void)headerViewDidClickedNameView:(XSHeaderView *)headerView;
@end
@interface XSHeaderView : UITableViewHeaderFooterView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView;
@property (nonatomic, strong) XSHomeStasges *stagesModel;
@property (nonatomic, assign) NSUInteger section;
@property (nonatomic, weak) id<XSHeaderViewDelegate> delegate;
@end
#import "XSHeaderView.h"
@interface XSHeaderView()
@property (nonatomic, weak) UILabel *desLabel;
@property (nonatomic, weak) UILabel *numLabel;
@property (nonatomic, weak) UIButton *nameBtn;
@property (nonatomic, weak) UIImageView *arrowView;
@property (nonatomic, weak) UIView *line;
@end
@implementation XSHeaderView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
static NSString *ID = @"header";
XSHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (header == nil) {
header = [[XSHeaderView alloc] initWithReuseIdentifier:ID];
}
return header;
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
// 1.添加按钮
UIButton *nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
nameBtn.backgroundColor = [UIColor whiteColor];
[nameBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
// 设置按钮的内容左对齐
nameBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[nameBtn addTarget:self action:@selector(nameBtnClick) forControlEvents:UIControlEventTouchUpInside];
// 设置按钮内部的imageView的内容模式为居中
nameBtn.imageView.contentMode = UIViewContentModeCenter;
// 超出边框的内容不需要裁剪
nameBtn.imageView.clipsToBounds = NO;
[self.contentView addSubview:nameBtn];
self.nameBtn = nameBtn;
UILabel *numLabel = [[UILabel alloc] init];
numLabel.layer.cornerRadius = 12.5;
numLabel.clipsToBounds = YES;
numLabel.textAlignment = NSTextAlignmentCenter;
numLabel.backgroundColor = [UIColor colorWithHex:0x1CBF74];
numLabel.textColor = [UIColor whiteColor];
[self.contentView addSubview:numLabel];
self.numLabel = numLabel;
// 2.标题描述
UILabel *desLabel = [[UILabel alloc] init];
desLabel.textAlignment = NSTextAlignmentLeft;
desLabel.textColor = [UIColor colorWithHex:0x1D2C2B];
desLabel.font = [UIFont systemFontOfSize:16];
[self.contentView addSubview:desLabel];
self.desLabel = desLabel;
//3.箭头
UIImageView* arrowView = [[UIImageView alloc] init];
arrowView.image = [UIImage imageNamed:@"section_open"];
[self.contentView addSubview:arrowView];
self.arrowView = arrowView;
UIView* line = [[UIView alloc]init];
line.backgroundColor = [UIColor colorWithHex:0xdddddd];
[self.contentView addSubview:line];
self.line = line;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 1.设置按钮的frame
self.nameBtn.frame = self.bounds;
// num frame
CGFloat numY = 25;
CGFloat numH = 25;
CGFloat numW = 25;
CGFloat numX = 12;
self.numLabel.frame = CGRectMake(numX, numY, numW, numH);
// 2.设置描述frame
CGFloat countY = 0;
CGFloat countH = self.frame.size.height;
CGFloat countW = 250;
CGFloat countX = 52;
self.desLabel.frame = CGRectMake(countX, countY, countW, countH);
//3.arrow
CGFloat arrowY = 33;
CGFloat arrowH = 7;
CGFloat arrowW = 13;
CGFloat arrowX = self.frame.size.width - 10 - arrowW;
self.arrowView.frame = CGRectMake(arrowX, arrowY, arrowW, arrowH);
self.line.frame = CGRectMake(12, 74, self.frame.size.width, 0.5);
}
-(void)setStagesModel:(XSHomeStasges *)stagesModel{
_stagesModel = stagesModel;
self.numLabel.text =stagesModel.sort;
// 2.设置阶段描述
self.desLabel.text = stagesModel.name;
if (self.stagesModel.isOpen) {
_arrowView.image = [UIImage imageNamed:@"section_close"];
}else{
_arrowView.image = [UIImage imageNamed:@"section_open"];
}
}
- (void)nameBtnClick
{
if (!self.stagesModel.isOpen) {
self.arrowView.image = [UIImage imageNamed:@"section_close"];
}else{
self.arrowView.image = [UIImage imageNamed:@"section_open"];
}
// 2.刷新表格
if ([self.delegate respondsToSelector:@selector(headerViewDidClickedNameView:)]) {
[self.delegate headerViewDidClickedNameView:self];
}
}
@end


网友评论