#import <UIKit/UIKit.h>
@class CZFriendGroup;
@class CZHeaderView;
@protocol CZHeaderViewDelegate <NSObject>
@optional
- (void)headerViewDidClickedNameView:(CZHeaderView *)headerView;
@end
@interface CZHeaderView : UITableViewHeaderFooterView
@property (nonatomic, strong) CZFriendGroup *friendGroup;
@property (nonatomic, weak) id<CZHeaderViewDelegate> delegate;
+ (instancetype)headerViewWithTableView:(UITableView *)tableView;
@endide
#import "CZHeaderView.h"
#import "CZFriendGroup.h"
@interface CZHeaderView ()
@property (nonatomic, weak) UIButton *nameView;
@property (nonatomic, weak) UILabel *countView;
@end
@implementation CZHeaderView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
static NSString *reuseId = @"header";
CZHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuseId];
if (header == nil) {
header = [[self alloc] initWithReuseIdentifier:reuseId];
}
return header;
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
//
UIButton *nameView = [UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:nameView];
self.nameView = nameView;
[nameView setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
[nameView setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
[nameView setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
[nameView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
nameView.titleLabel.font = [UIFont systemFontOfSize:25];
nameView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
nameView.contentEdgeInsets = UIEdgeInsetsMake(10, 15, 0, 0);
nameView.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
nameView.imageView.contentMode = UIViewContentModeCenter;
nameView.imageView.clipsToBounds = NO;
[nameView addTarget:self action:@selector(didClickedNameView:) forControlEvents:UIControlEventTouchUpInside];
UILabel *countView = [[UILabel alloc] init];
[self.contentView addSubview:countView];
self.countView = countView;
countView.textAlignment = NSTextAlignmentRight;
countView.font = [UIFont systemFontOfSize:15];
countView.textColor = [UIColor grayColor];
}
return self;
}
- (void)didClickedNameView:(UIButton *)sender
{
self.friendGroup.expend = !self.friendGroup.isExpend;
if (self.friendGroup.isExpend) {
self.nameView.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
}else{
self.nameView.imageView.transform = CGAffineTransformMakeRotation(0);
}
if ([self.delegate respondsToSelector:@selector(headerViewDidClickedNameView:)]) {
[self.delegate headerViewDidClickedNameView:self];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.nameView.frame = self.bounds;
self.countView.frame = CGRectMake(self.bounds.size.width - 150 - 10, 0, 150, 44);
}
- (void)setFriendGroup:(CZFriendGroup *)friendGroup
{
_friendGroup = friendGroup;
[self.nameView setTitle:friendGroup.name forState:UIControlStateNormal];
self.countView.text = [NSString stringWithFormat:@"%d/%ld",friendGroup.online,friendGroup.friends.count];
if (self.friendGroup.isExpend) {
self.nameView.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
}else{
self.nameView.imageView.transform = CGAffineTransformMakeRotation(0);
}
}
@end
atom
//點M文件代理
#import "CZFriendGroup.h"
#import "CZFriend.h"
@implementation CZFriendGroup
- (instancetype)initWithDic:(NSDictionary *)dic
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
+ (instancetype)friendGroupWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
+ (NSArray *)friendGroupsList
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@"plist"];
NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *friendGroups = [NSMutableArray array];
for (NSDictionary *dic in dicArray) {
CZFriendGroup *friendGroup = [[CZFriendGroup alloc] initWithDic:dic];
NSMutableArray *tmpArray = [NSMutableArray array];
for (NSDictionary *friendDic in friendGroup.friends) {
CZFriend *friend = [CZFriend friendWithDic:friendDic];
[tmpArray addObject:friend];
}
friendGroup.friends = tmpArray;
[friendGroups addObject:friendGroup];
}
return friendGroups;
}
@end
orm
#import "ViewController.h"
#import "CZFriend.h"
#import "CZFriendGroup.h"
#import "CZHeaderView.h"
@interface ViewController () <CZHeaderViewDelegate>
@property (nonatomic, strong) NSArray *friendGroups;
@end
@implementation ViewController
- (BOOL)prefersStatusBarHidden
{
return YES;
}
// 懶加載
- (NSArray *)friendGroups
{
if (_friendGroups == nil) {
_friendGroups = [CZFriendGroup friendGroupsList];
}
return _friendGroups;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.sectionHeaderHeight = 44;
}
#pragma mark - 數據源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.friendGroups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
CZFriendGroup *group = self.friendGroups[section];
return group.isExpend ? group.friends.count : 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1
static NSString *reuseId = @"friend";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
}
//2
CZFriendGroup *group = self.friendGroups[indexPath.section];
CZFriend *friend = group.friends[indexPath.row];
cell.textLabel.text = friend.name;
cell.detailTextLabel.text = friend.intro;
cell.imageView.image = [UIImage imageNamed:friend.icon];
cell.textLabel.textColor = friend.isVip ? [UIColor redColor]:[UIColor blackColor];
return cell;
}
#pragma mark - 代理的方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//1
CZHeaderView *headerView = [CZHeaderView headerViewWithTableView:tableView];
headerView.delegate = self;
headerView.tag = section;
//2
CZFriendGroup *group = self.friendGroups[section];
headerView.friendGroup = group;
//3
return headerView;
}
#pragma mark - headerView的代理方法
- (void)headerViewDidClickedNameView:(CZHeaderView *)headerView
{
// [self.tableView reloadData];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:headerView.tag];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
}
@endip