---------- CZGroupHeader.h ----------
#import
@class CZFriendGroup, CZGroupHeader;
@protocol CZGroupHeaderDelegate <NSObject>
@optional
- (void)groupHeaderClick:(CZGroupHeader *)header;
@end
@interface CZGroupHeader : UITableViewHeaderFooterView
+ (instancetype)headerWithTableView:(UITableView *)tableView;
@property (strong, nonatomic) CZFriendGroup *group;
@property (weak, nonatomic) id<CZGroupHeaderDelegate> delegate;
@end
---------- CZGroupHeader.m ----------
#import "CZGroupHeader.h"
#import "CZFriendGroup.h"
@interface CZGroupHeader()
@property (weak, nonatomic) UIButton *nameBtn;
@property (weak, nonatomic) UILabel *onlineLabel;
@end
@implementation CZGroupHeader
+ (instancetype)headerWithTableView:(UITableView *)tableView
{
static NSString *ID = @"group";
CZGroupHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (header == nil) {
header = [[CZGroupHeader alloc] initWithReuseIdentifier:ID];
CGFloat headerW = tableView.frame.size.width;
header.frame = CGRectMake(0, 0, headerW, 44);
}
return header;
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier])
{
UIButton *nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[nameBtn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
[nameBtn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
[nameBtn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
nameBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
nameBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[nameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
nameBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
nameBtn.titleLabel.font = [UIFont systemFontOfSize:17];
[nameBtn addTarget:self action:@selector(nameClick) forControlEvents:UIControlEventTouchUpInside];
nameBtn.imageView.contentMode = UIViewContentModeCenter;
nameBtn.imageView.clipsToBounds = NO;
[self addSubview:nameBtn];
self.nameBtn = nameBtn;
UILabel *onlineLabel = [[UILabel alloc] init];
onlineLabel.textColor = [UIColor grayColor];
onlineLabel.textAlignment = NSTextAlignmentRight;
onlineLabel.backgroundColor = [UIColor clearColor];
[self addSubview:onlineLabel];
self.onlineLabel = onlineLabel;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.nameBtn.frame = self.bounds;
CGFloat onlineY = 0;
CGFloat onlineH = self.frame.size.height;
CGFloat onlineW = 150;
CGFloat onlineX = self.frame.size.width - onlineW - 10;
self.onlineLabel.frame = CGRectMake(onlineX, onlineY, onlineW, onlineH);
}
- (void)setGroup:(CZFriendGroup *)group
{
_group = group;
[self.nameBtn setTitle:group.name forState:UIControlStateNormal];
self.onlineLabel.text = [NSString stringWithFormat:@"%d/%ld", group.online, group.friends.count];
}
- (void)nameClick
{
self.group.open = !self.group.open;
if ([self.delegate respondsToSelector:@selector(groupHeaderClick:)])
{
[self.delegate groupHeaderClick:self];
}
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
if (self.group.open)
{
self.nameBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
} else {
self.nameBtn.imageView.transform = CGAffineTransformIdentity;
}
}
@end
---------- CZFriendCell.h ----------
#import
#import "CZFriend.h"
@interface CZFriendCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@property (strong, nonatomic) CZFriend *friendData;
@end
---------- CZFriendCell.m ----------
#import "CZFriendCell.h"
@implementation CZFriendCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"friend";
CZFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil)
{
cell = [[CZFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.textLabel.font = [UIFont boldSystemFontOfSize:14];
}
return self;
}
- (void)setFriendData:(CZFriend *)friendData
{
_friendData = friendData;
self.imageView.image = [UIImage imageNamed:friendData.icon];
self.textLabel.text = friendData.name;
self.textLabel.textColor = friendData.vip ? [UIColor redColor] : [UIColor blackColor];
self.detailTextLabel.text = friendData.intro;
}
@end