实现QQ 好友列表
2014-12-01 15:43
316 查看
实现qq好友列表功能:
1,数据分两部分 一个为 FriendGroup 和Friend
FriendGroup ----->(many) Friend
FriendGroup.h
FriendGroup.m
这行代码主要是为了让dict 的key 跟 FriendGroup 的属性一致 取出Value 自动去设置属性的值。
Friend.h 和 Friend.m的代码跟以上代码类似
ViewController.m 添加UITableView
_tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height-40)];
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.backgroundColor=[UIColor clearColor];
[self.view addSubview:_tableView];
实现UITableView 的代理方法 :
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
FriendGroup * fg=_dataArray[section];
return fg.isOpened ? fg.friends.count : 0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
HeadView *headView = [HeadView headViewWithTableView:tableView];
headView.delegate = self;
headView.friendGroup = _dataArray[section];
return headView;
}
- (void)clickHeadView
{
[_tableView reloadData];
}
每次点击HeadView 时都要调用一次 reloadData method
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
FriendGroup * fg=_dataArray[section];
return fg.isOpened ? fg.friends.count : 0 ;
}
根据状态判断是否显示显示出好友
HeadView.h
#import <UIKit/UIKit.h>
@class FriendGroup;
@protocol HeadViewDelegate <NSObject>
@optional
- (void)clickHeadView;
@end
@interface HeadView : UITableViewHeaderFooterView
@property (nonatomic, strong) FriendGroup *friendGroup;
@property (nonatomic, weak) id<HeadViewDelegate> delegate;
+ (instancetype)headViewWithTableView:(UITableView *)tableView;
@end
HeadView.m
#import "HeadView.h"
#import "FriendGroup.h"
@interface HeadView()
{
UIButton *_bgButton;
UILabel *_numLabel;
}
@end
@implementation HeadView
+ (instancetype)headViewWithTableView:(UITableView *)tableView
{
static NSString *headIdentifier = @"header";
HeadView *headView = [tableView dequeueReusableCellWithIdentifier:headIdentifier];
if (headView == nil) {
headView = [[HeadView alloc] initWithReuseIdentifier:headIdentifier];
}
return headView;
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bgButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
[bgButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
[bgButton setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
[bgButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
bgButton.imageView.contentMode = UIViewContentModeCenter;
bgButton.imageView.clipsToBounds = NO;
bgButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
bgButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
bgButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[bgButton addTarget:self action:@selector(headBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:bgButton];
_bgButton = bgButton;
UILabel *numLabel = [[UILabel alloc] init];
numLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:numLabel];
_numLabel = numLabel;
}
return self;
}
- (void)headBtnClick
{
_friendGroup.opened = !_friendGroup.isOpened;
if ([_delegate respondsToSelector:@selector(clickHeadView)]) {
[_delegate clickHeadView];
}
}
- (void)setFriendGroup:(FriendGroup *)friendGroup
{
_friendGroup = friendGroup;
[_bgButton setTitle:friendGroup.name forState:UIControlStateNormal];
_numLabel.text = [NSString stringWithFormat:@"%ld/%lu", (long)friendGroup.online, (unsigned long)friendGroup.friends.count];
}
- (void)didMoveToSuperview
{
_bgButton.imageView.transform = _friendGroup.isOpened ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}
- (void)layoutSubviews
{
[super layoutSubviews];
_bgButton.frame = self.bounds;
_numLabel.frame = CGRectMake(self.frame.size.width - 70, 0, 60, self.frame.size.height);
}
@end
1,数据分两部分 一个为 FriendGroup 和Friend
FriendGroup ----->(many) Friend
FriendGroup.h
#import <Foundation/Foundation.h> @interface FriendGroup : NSObject @property (nonatomic, strong) NSArray *friends; @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) NSInteger online; @property (nonatomic, assign, getter = isOpened) BOOL opened; - (instancetype)initWithDict:(NSDictionary *)dict; @end
FriendGroup.m
#import "FriendGroup.h" #import "Friend.h" @implementation FriendGroup - (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; NSMutableArray *tempArray = [NSMutableArray array]; for (NSDictionary *dict in _friends) { Friend *friend = [[Friend alloc] initWithDic:dict]; [tempArray addObject:friend]; } _friends = tempArray; } return self; } @end
[self setValuesForKeysWithDictionary:dict]
这行代码主要是为了让dict 的key 跟 FriendGroup 的属性一致 取出Value 自动去设置属性的值。
Friend.h 和 Friend.m的代码跟以上代码类似
ViewController.m 添加UITableView
_tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height-40)];
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.backgroundColor=[UIColor clearColor];
[self.view addSubview:_tableView];
实现UITableView 的代理方法 :
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
FriendGroup * fg=_dataArray[section];
return fg.isOpened ? fg.friends.count : 0 ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
HeadView *headView = [HeadView headViewWithTableView:tableView];
headView.delegate = self;
headView.friendGroup = _dataArray[section];
return headView;
}
- (void)clickHeadView
{
[_tableView reloadData];
}
每次点击HeadView 时都要调用一次 reloadData method
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
FriendGroup * fg=_dataArray[section];
return fg.isOpened ? fg.friends.count : 0 ;
}
根据状态判断是否显示显示出好友
HeadView.h
#import <UIKit/UIKit.h>
@class FriendGroup;
@protocol HeadViewDelegate <NSObject>
@optional
- (void)clickHeadView;
@end
@interface HeadView : UITableViewHeaderFooterView
@property (nonatomic, strong) FriendGroup *friendGroup;
@property (nonatomic, weak) id<HeadViewDelegate> delegate;
+ (instancetype)headViewWithTableView:(UITableView *)tableView;
@end
HeadView.m
#import "HeadView.h"
#import "FriendGroup.h"
@interface HeadView()
{
UIButton *_bgButton;
UILabel *_numLabel;
}
@end
@implementation HeadView
+ (instancetype)headViewWithTableView:(UITableView *)tableView
{
static NSString *headIdentifier = @"header";
HeadView *headView = [tableView dequeueReusableCellWithIdentifier:headIdentifier];
if (headView == nil) {
headView = [[HeadView alloc] initWithReuseIdentifier:headIdentifier];
}
return headView;
}
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
UIButton *bgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[bgButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
[bgButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
[bgButton setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
[bgButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
bgButton.imageView.contentMode = UIViewContentModeCenter;
bgButton.imageView.clipsToBounds = NO;
bgButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
bgButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
bgButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
[bgButton addTarget:self action:@selector(headBtnClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:bgButton];
_bgButton = bgButton;
UILabel *numLabel = [[UILabel alloc] init];
numLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:numLabel];
_numLabel = numLabel;
}
return self;
}
- (void)headBtnClick
{
_friendGroup.opened = !_friendGroup.isOpened;
if ([_delegate respondsToSelector:@selector(clickHeadView)]) {
[_delegate clickHeadView];
}
}
- (void)setFriendGroup:(FriendGroup *)friendGroup
{
_friendGroup = friendGroup;
[_bgButton setTitle:friendGroup.name forState:UIControlStateNormal];
_numLabel.text = [NSString stringWithFormat:@"%ld/%lu", (long)friendGroup.online, (unsigned long)friendGroup.friends.count];
}
- (void)didMoveToSuperview
{
_bgButton.imageView.transform = _friendGroup.isOpened ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}
- (void)layoutSubviews
{
[super layoutSubviews];
_bgButton.frame = self.bounds;
_numLabel.frame = CGRectMake(self.frame.size.width - 70, 0, 60, self.frame.size.height);
}
@end
相关文章推荐
- 自定义UITableViewCell,实现类似QQ好友列表
- Python 最近因开发项目的需要,有一个需求,就是很多SNS网站都有的通过 Email地址 导入好友列表,不过这次要导入的不是Email 列表,而是QQ的好友列表。 实现方式: 通过goog
- 自定义ExpandableListView 实现像QQ好友列表一样的功能
- UI基础--UITableView实现仿QQ好友列表页面
- Android初级教程_用ExpandableListView实现类似QQ好友列表
- QQ好友列表树形列表简单实现方式
- Android开发学习之QQ好友列表的实现
- 用ExpandableListView实现类似QQ好友列表
- 在Fragment里面使用ExpandableListView实现仿qq好友列表
- Android初级教程_用ExpandableListView实现类似QQ好友列表
- IOS详解TableView —— QQ好友列表的实现
- QQ - iPhone 风格的好友列表实现
- 基于Qt的类似QQ好友列表抽屉效果的实现
- 用ExpandableListView实现类似QQ好友列表
- 用ExpandableListView实现类似QQ好友列表
- swing jtree实现叶子节点闪动效果类似于QQ好友列表
- Qt实现QQ好友下拉列表
- (二十七)QQ好友列表的实现
- Android初级教程_用ExpandableListView实现类似QQ好友列表
- 65 Android ExpandableListView (QQ好友列表的实现)