700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 实现QQ 好友列表

实现QQ 好友列表

时间:2018-09-21 21:14:31

相关推荐

实现QQ 好友列表

实现qq好友列表功能:

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。