*****HMViewController.m
#import "HMViewController.h" #import "HMFriendsGroupModel.h" #import "HMFriendsModel.h" #import "HMHeaderView.h" @interface HMViewController ()<HMHeaderViewDelegate> //存放fiend内容 @property (nonatomic, strong)NSArray *friendsArr; @end @implementation HMViewController /** * 当一个控件没有显示出来的时候 1. 父控件的frame 2. 当前控件frame 3. 当前控件 hidden (是否为yes ) 4. alpha < = 0.01 */ - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 当 cell header footer 是固定值的时候 使用以下操作 //frame 是动态的时候 不便调用 // self.tableView.rowHeight = 40; // self.tableView.sectionHeaderHeight = 44; // self.tableView.sectionFooterHeight = 44; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiClcik) name:@"friend" object:nil]; } - (void)notiClcik { [self.tableView reloadData]; } // 这个方法是在控制器销毁的时候调用 - (void)dealloc { //非ARC 必须调取这个方法 // [super dealloc]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (NSArray *)friendsArr { if (_friendsArr == nil) { NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil]]; NSMutableArray *friendsArray = [NSMutableArray array]; for (NSDictionary *dict in arr) { HMFriendsGroupModel *model = [HMFriendsGroupModel friendsGroupWithDict:dict]; [friendsArray addObject:model]; } _friendsArr = friendsArray; } return _friendsArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //隐藏状态栏 - (BOOL)prefersStatusBarHidden { return YES; } /** * 设置header 的tiltle * * @return header 头部显示内容 */ //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section //{ // // HMFriendsGroupModel *group = self.friendsArr[section]; // // return group.name; //} /** * 设置headerVIew 的高度 * */ - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44; } #pragma mark - header的代理方法 //- (void)headerView:(HMHeaderView *)view //{ // [self.tableView reloadData]; //} #pragma mark - 数据源方法 多少组 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //取出当前section 的model HMFriendsGroupModel *model = self.friendsArr[section]; return model.open ? model.friends.count:0; } // 返回一个header 给tableview 显示 // 所有UIView 当只是做了init 操作的时候 然后做一些 关于frame 的操作, 这个frame的值肯定 没有值的 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { HMHeaderView *header = [HMHeaderView headerViewWith:tableView]; HMFriendsGroupModel *model = self.friendsArr[section]; //让控制器充当headerView 的代理 // header.delegate = self; // sender 传过来的参数 ^ 是不block 象征 {};做相应操作 // header.block = ^(id sender){ // [tableView reloadData]; // // NSLog(@"------%@",sender); // // }; header.group = model; // return [UIButton buttonWithType:UIButtonTypeContactAdd]; return header; } //tableview 只中显示多少组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.friendsArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"friends"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //1 取出当前section model HMFriendsGroupModel *group = self.friendsArr[indexPath.section]; //2 取出当前section model 中的 row 行 HMFriendsModel *model = group.friends[indexPath.row]; cell.textLabel.text = model.name; //设置cell text 的颜色 cell.textLabel.textColor = model.vip?[UIColor redColor]:[UIColor blackColor]; cell.imageView.image = [UIImage imageNamed:model.icon]; return cell; } @end
#import <UIKit/UIKit.h> @interface HMViewController : UITableViewController @end
******model HMFriendsGroupModel.h
#import <Foundation/Foundation.h> @interface HMFriendsGroupModel : NSObject //友源函数 friend 是个c++的关键字 /** * 存放每行显示的内容 */ @property (nonatomic, strong)NSArray *friends; /** * headerView 显示的内容 */ @property (nonatomic, copy)NSString *name; /** * 在线人数 */ @property (nonatomic, assign)int online; /** * 展开的时候 */ @property (nonatomic, assign)BOOL open; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)friendsGroupWithDict:(NSDictionary *)dict; @end
******model HMFriendsGroupModel.m
#import "HMFriendsGroupModel.h" #import "HMFriendsModel.h" @implementation HMFriendsGroupModel - (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { //注入所有值 [self setValuesForKeysWithDictionary:dict]; NSMutableArray *arr = [NSMutableArray array]; for (NSDictionary *dict in self.friends) { HMFriendsModel *model = [HMFriendsModel friendsWithDict:dict]; [arr addObject:model]; } self.friends = arr; } return self; } + (instancetype)friendsGroupWithDict:(NSDictionary *)dict { return [[self alloc]initWithDict:dict]; } @end
***HMFriendsModel.h
#import <Foundation/Foundation.h> @interface HMFriendsModel : NSObject /** * 名称 */ @property (nonatomic, copy)NSString *name; /** * 用户图片 */ @property (nonatomic, copy)NSString *icon;; /** * 个性签名 */ @property (nonatomic, copy)NSString *intro;; /** * 是否是vip */ @property (nonatomic, assign,getter = isVip)BOOL vip; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)friendsWithDict:(NSDictionary *)dict; @end
***HMFriendsModel.m
#import "HMFriendsModel.h" @implementation HMFriendsModel - (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } + (instancetype)friendsWithDict:(NSDictionary *)dict { return [[self alloc]initWithDict:dict]; } @end
**HMHeaderView.h
#import <UIKit/UIKit.h> @class HMHeaderView; typedef void(^HMHeaderViewBlock)(id); //biock @protocol HMHeaderViewDelegate <NSObject> //代理 @optional - (void)headerView:(HMHeaderView *)view; @end @class HMFriendsGroupModel; @interface HMHeaderView : UITableViewHeaderFooterView + (instancetype)headerViewWith:(UITableView *)tableview; @property (nonatomic, assign)id<HMHeaderViewDelegate> delegate; @property (nonatomic, strong)HMFriendsGroupModel *group; @property (nonatomic, copy)HMHeaderViewBlock block; @end
**HMHeaderView.m
#import "HMHeaderView.h" #import "HMFriendsGroupModel.h" @interface HMHeaderView () @property (nonatomic, weak)UIButton *nameBtn; @property (nonatomic, weak)UILabel *textLbl; @end @implementation HMHeaderView + (instancetype)headerViewWith:(UITableView *)tableview { static NSString *ID = @"headerView"; //首先看缓存池中是否存在headerView,如果存在的 直接取出来用 HMHeaderView *header = [tableview dequeueReusableHeaderFooterViewWithIdentifier:ID]; if (header == nil) { //如果不存在 重新创建一个 header = [[HMHeaderView alloc]initWithReuseIdentifier:ID]; } return header; } - (void)setGroup:(HMFriendsGroupModel *)group { //1. 必须做的操作 _group = group; [self.nameBtn setTitle:group.name forState:UIControlStateNormal]; //显示在线人数 self.textLbl.text = [NSString stringWithFormat:@"%d/%d",group.online,group.friends.count]; } // 当headerview 上子控件只需 做一次操作的 或者 要显示出来的 就写在以下方法中 - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithReuseIdentifier:reuseIdentifier]) { // Custom 相当 [[UIButton alloc]init]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; //btn 上面有一个imageView [btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //设置按钮内容的居左显示 btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; //设置按钮的内边距 btn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); //设置按钮 label 的 内边距 btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); //按钮内部 imageview 的内边距 // btn.imageEdgeInsets //居中显示 btn.imageView.contentMode = UIViewContentModeCenter; //不予许剪切超出部分 btn.imageView.clipsToBounds = NO; [btn addTarget:self action:@selector(nameBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:btn]; self.nameBtn = btn; NSLog(@"---------%@",NSStringFromCGRect(self.contentView.frame)); UILabel *lable = [[UILabel alloc]init]; //居右显示 lable.textAlignment = NSTextAlignmentRight; [self.contentView addSubview:lable]; self.textLbl = lable; } return self; } /** * 代理方法 */ - (void)nameBtnClick { self.group.open = !self.group.open; // if ([self.delegate respondsToSelector:@selector(headerView:)]) { //代理 // [self.delegate headerView:self]; // } if (self.block) { self.block(self); } [[NSNotificationCenter defaultCenter] postNotificationName:@"friend" object:self userInfo:nil]; //通知 NSLog(@"----------------"); } /** * 当 当前的view 加载到父控件的时候调用 */ - (void)didMoveToSuperview { //每次当控件加载到父控件的时候都会调用这个方法,包括init 完一次就会调用一次 if (self.group.open) { self.nameBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2); NSLog(@"999999999"); }else{ self.nameBtn.imageView.transform = CGAffineTransformMakeRotation(0); } } /** * 当 当前的view 的frame 发生一些改变的时候 调用次方法 重新布局 内部的子控件 */ - (void)layoutSubviews { self.nameBtn.frame = self.bounds; //获取屏幕的宽度 // CGFloat screenW = [[UIScreen mainScreen] bounds].size.width; CGFloat lblY = 0; CGFloat lblW = 150; CGFloat lblh = self.frame.size.height; CGFloat lblX = self.frame.size.width - lblW - 10; self.textLbl.frame = CGRectMake(lblX, lblY, lblW, lblh); } @end