1 // 2 // FriendGroup.h 3 // FriendsList 4 // 5 // Created by hellovoidworld on 14/12/12. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h>10 11 @class Friend;12 13 @interface FriendGroup : NSObject14 15 /** 好友组 */16 @PRoperty(nonatomic, strong) NSArray *friends;17 18 /** 好友组名 */19 @property(nonatomic, copy) NSString *name;20 21 /** 在线人数 */22 @property(nonatomic, assign) int online;23 24 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;25 + (instancetype) friendGroupWithDictionary:(NSDictionary *) dictionary;26 27 @end
1 /** 加载数据 */2 - (void)setFriendGroup:(FriendGroup *)friendGroup
1 /** 子控件布局方法 2 在init的时候,只分配的内存,没有初始化控件的尺寸,在此处header已经有了位置尺寸了 3 */ 4 - (void)layoutSubviews { 5 // 必须先调用父类的方法 6 [super layoutSubviews]; 7 8 // 1.背景 9 self.headerButtonView.frame = self.bounds;10 11 // 2.在线人数/组内总人数12 CGFloat countWidth = 150;13 CGFloat countHeight = self.frame.size.height;14 CGFloat countX = self.frame.size.width - 10 - countWidth;15 CGFloat countY = 0;16 self.onlineCountView.frame = CGRectMake(countX, countY, countWidth, countHeight);17 }
1 /** 自定义每个section的头部 */2 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {3 FriendHeader *header = [FriendHeader friendHeaderWithTableView:self.tableView];4 header.friendGroup = self.friendGroups[section];5 return header;6 }
1 /** 自定义构造方法 */ 2 + (instancetype) cellWithTableView:(UITableView *) tableView { 3 static NSString *ID = @"friendCell"; 4 FriendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 5 6 if (nil == cell) { 7 cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; 8 } 9 10 return cell;11 }12 13 /** 加载数据 */14 - (void)setFriendData:(Friend *)friendData {15 _friendData = friendData;16 17 self.imageView.image = [UIImage imageNamed:friendData.icon];18 self.textLabel.text = friendData.name;19 self.textLabel.textColor = friendData.isVip?[UIColor redColor]:[UIColor blackColor];20 self.detailTextLabel.text = friendData.intro;21 }
1 /** 是否伸展显示好友 */2 @property(nonatomic, assign, getter=isOpened) BOOL opened;
1 // 1.4点击事件2 [headerButtonView addTarget:self action:@selector(headerClicked) forControlEvents:UIControlEventTouchUpInside];
1 /** 点击事件 */ 2 - (void) headerClicked { 3 // 1.伸展、隐藏组内好友 4 self.friendGroup.opened = !self.friendGroup.isOpened; 5 6 // 2.刷新tableView 7 if ([self.delegate respondsToSelector:@selector(friendHeaderDidClickedHeader:)]) { 8 [self.delegate friendHeaderDidClickedHeader:self]; 9 }10 }
1 @protocol FriendHeaderDelegate <NSObject>2 /** header被点击的代理方法 */3 @optional4 - (void) friendHeaderDidClickedHeader:(FriendHeader *) header;5 6 @end
1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {2 FriendGroup *group = self.friendGroups[section];3 // 先检查模型数据内的伸展标识4 return group.isOpened? group.friends.count : 0;5 }
1 #pragma mark - FriendHeaderDelegate方法2 - (void)friendHeaderDidClickedHeader:(FriendHeader *)header {3 // 刷新数据4 [self.tableView reloadData];5 }
1 /**2 被加到父控件之前 3 由于tableView刷新数据后,所有header会被重新创建,所以要在这里对箭头朝向做出修改4 */5 - (void)didMoveToSuperview {6 // 改变箭头朝向,顺时针旋转90度7 CGFloat rotation = self.friendGroup.isOpened? M_PI_2 : 0;8 self.headerButtonView.imageView.transform = CGAffineTransformMakeRotation(rotation);9 }
1 // 改变箭头填充方式为居中2 [headerButtonView.imageView setContentMode:UIViewContentModeCenter];3 // 不需要裁剪箭头图片的边界4 [headerButtonView.imageView setClipsToBounds:NO];
新闻热点
疑难解答