iOS-QQ好友列表实现
0、QQ好友列表实现
0、首先说说实现思路
- 自定义
UITableView,每一个分组都是一个UITableViewHeaderFooterView,然后自定义cell,这里分组的实现主要是自定义UITableViewHeaderFooterView,这个折叠效果主要靠这个header的响应
- 自定义
1、实现数据源方法
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// 返回分组个数
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取出具体的组数,然后再取出每组的内容
SLQFriendGroup *group = self.friendList[section];
// 根据分组是否打开情况确定如何显示分组,默认关闭
return group.friends.count;
}
/**
* 返回自定义cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1、生成cell
SLQFriendCell *cell = [SLQFriendCell cellWithTableView:tableView];
// 3、返回cell
return cell;
}
2、模型定义
- 2.0、在控制器定义个数组,存放解析的plist数据
/**朋友数组*/ @property (nonatomic, copy) NSArray *friendList;
- 2.1、自定义分组模型
@class SLQFriend;
@interface SLQFriendGroup : NSObject
/**分组名称*/
@property (nonatomic, copy) NSString *name;
/**在线人数*/
@property (nonatomic, assign) int online;
/**分组中用户数量*/
@property (nonatomic, copy) NSArray *friends;
/**分组是隐藏还是展开*/
@property (nonatomic, assign,getter=isOpened) BOOL opened;
- (instancetype)initWithDictionary:(NSDictionary *)dict;
+ (instancetype)FriendGroupWithDictionary:(NSDictionary *)dict;
@end
// 实现文件
#import "SLQFriendGroup.h"
#import "SLQFriend.h"
@implementation SLQFriendGroup
- (instancetype)initWithDictionary:(NSDictionary *)dict
{
if (self = [super init]) {
// 1、KVC字典转模型
[self setValuesForKeysWithDictionary:dict];
// 2、然后再转换数组中信息
NSMutableArray *groupFriends = [NSMutableArray array];
for (NSDictionary *dict in self.friends) {
// 3、字典转模型
SLQFriend *friend = [SLQFriend friendWithDict:dict];
[groupFriends addObject:friend];
}
self.friends = groupFriends;
}
return self;
}
+ (instancetype)FriendGroupWithDictionary:(NSDictionary *)dict
{
return [[self alloc] initWithDictionary:dict];
}
@end
- 2.2、朋友模型
#import <Foundation/Foundation.h>
@interface SLQFriend : NSObject
/**头像*/
@property (nonatomic, copy) NSString *icon;
/**昵称*/
@property (nonatomic, copy) NSString *name;
/**好友签名*/
@property (nonatomic, copy) NSString *intro;
/**是否是vip*/
@property (nonatomic, assign, getter=isVip) BOOL vip;
- (instancetype)initFriendWithDict:(NSDictionary *)dict;
+ (instancetype)friendWithDict:(NSDictionary *)dict;
@end
// 实现文件
@implementation SLQFriend
- (instancetype)initFriendWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
// kvc字典转模型
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)friendWithDict:(NSDictionary *)dict
{
return [[self alloc] initFriendWithDict:dict];
}
@end
3、字典转模型
- 在控制器里加载字典数据并转换成模型
#pragma mark - 懒加载
- (NSArray *)friendList
{
if (!_friendList) {
_friendList = [NSArray array];
// 从plist读取用户信息
NSString *path = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
NSArray *friends = [NSArray arrayWithContentsOfFile:path];
/**
* 字典转模型
*/
NSMutableArray *mutableFriends = [NSMutableArray array];
for (NSDictionary *dict in friends) {
SLQFriendGroup *group = [SLQFriendGroup FriendGroupWithDictionary:dict];
[mutableFriends addObject:group];
}
_friendList = mutableFriends;
}
return _friendList;
}
- 3.1、修改数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// 返回分组个数
return self.friendList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 取出具体的组数,然后再取出每组的内容
SLQFriendGroup *group = self.friendList[section];
// 根据分组是否打开情况确定如何显示分组,默认关闭
return group.isOpened ? group.friends.count : 0;
}
/**
* 返回自定义cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1、生成cell
SLQFriendCell *cell = [SLQFriendCell cellWithTableView:tableView];
// 2、传递SLQFriend模型
SLQFriendGroup *group = self.friendList[indexPath.section];
cell.Friend = group.friends[indexPath.row];
// 3、返回cell
return cell;
}
4、自定义cell
- 这个自定义cell就不多说了
#import <UIKit/UIKit.h>
@class SLQFriend;
@interface SLQFriendCell : UITableViewCell
/**模型数组*/
@property (nonatomic, strong) SLQFriend *Friend;
+ (SLQFriendCell *)cellWithTableView:(UITableView *)tableView;
@end
#import "SLQFriendCell.H"
#import "SLQFriendGroup.h"
#import "SLQFriend.h"
@implementation SLQFriendCell
/**
* setter方法
*/
- (void)setFriend:(SLQFriend *)Friend
{
_Friend = Friend;
// 更新数据到控件上
self.textLabel.text = Friend.name;
self.detailTextLabel.text = Friend.intro;
self.imageView.image = [UIImage imageNamed:Friend.icon];
self.textLabel.textColor = Friend.isVip ? [UIColor redColor] : [UIColor blackColor];
}
/**
* 返回cell
*/
+ (SLQFriendCell *)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"Cell";
SLQFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[SLQFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
return cell;
}

5、自定义HeaderFooterView
- 这个才是弹出列表的关键所在
#import <UIKit/UIKit.h>
@class SLQFriendGroup;
@interface SLQHeader : UITableViewHeaderFooterView
/**用户分组*/
@property (nonatomic, strong) SLQFriendGroup *friendGroup;
/** 按钮*/
@property (nonatomic, weak) UIButton *contentButton;
/** 标签*/
@property (nonatomic, weak) UILabel *onlineLabel;
+ (instancetype)headerWithTableView:(UITableView *)tableView;
@end
- 5.0、重写setter方法,设置数据
- (void)setFriendGroup:(SLQFriendGroup *)friendGroup
{
_friendGroup = friendGroup;
// 加空格,让图片和文字产生一个间隔
NSString *name = [NSString stringWithFormat:@" %@",friendGroup.name];
[self.contentButton setTitle:name forState:UIControlStateNormal];
[self.contentButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
self.onlineLabel.text = [NSString stringWithFormat:@"%zd/%zd",friendGroup.online,friendGroup.friends.count];
}
- 5.1、封装header生成操作,返回header
// 返回生成好的header
+ (instancetype)headerWithTableView:(UITableView *)tableView
{
static NSString *ID = @"header";
SLQHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
if (header == nil) {
header = [[SLQHeader alloc] initWithReuseIdentifier:ID];
}
return header;
}
- 5.2、布局子控件
- (void)layoutSubviews
{
[super layoutSubviews];
// 布局添加的子控件
// 1、背景
self.contentButton.frame = self.bounds;
// 稍微向右偏移一点
self.contentButton.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
// 2、标签:在线人数/组内总人数
NSInteger countWidth = 150;
NSInteger countHeight = self.bounds.size.height;
NSInteger countX = self.bounds.size.width - 10 - countWidth;
NSInteger countY = 0;
self.onlineLabel.frame = CGRectMake(countX, countY, countWidth, countHeight);
// 3、改变小箭头的方向
// 由于tableView刷新数据后,所有header会被重新创建,所以要在这里对箭头朝向做出修改
// 改变箭头朝向,顺时针旋转90度
CGFloat rotation = self.friendGroup.isOpened? M_PI_2 : 0;
self.contentButton.imageView.transform = CGAffineTransformMakeRotation(rotation);
}
- 5.3、自定义
initWithReuseIdentifier:ID方法,添加自定义控件
/** 重写初始化方法, 给header加上图标、组名、在线人数等子控件 */
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier])
{
// 1、添加按钮
UIButton *contentButton = [[UIButton alloc] init];
[self.contentView addSubview:contentButton];
// 1.0 添加背景图片
[contentButton setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
[contentButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
[contentButton setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
// 设置对齐方式
contentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 1.1 添加点击事件
[contentButton addTarget:self action:@selector(contentButtonClick:) forControlEvents:UIControlEventTouchUpInside];
_contentButton = contentButton;
// 2、最右边标签
UILabel *onlineLabel = [[UILabel alloc] init];
[self.contentView addSubview:onlineLabel];
[onlineLabel setTextAlignment:NSTextAlignmentRight];
_onlineLabel = onlineLabel;
}
return self;
}
- 5.4、响应header点击事件,点击过后展开或者收缩内部cell,这个牵涉到tableView的数据更新,那么可以将这个事件传递出去(使用代理)
- (void)contentButtonClick:(UIButton *)btn
{
// 隐藏\显示 好友
self.friendGroup.opened = !self.friendGroup.isOpened;
// 调用代理方法传递按钮点击消息
if ([self.delegate respondsToSelector:@selector(headerDidClicked:)]) {
[self.delegate headerDidClicked:self];
}
}
- 5.5、代理实现,只需实现一个方法就行,而且是可选的
@class SLQHeader;
@protocol SLQHeaderDelegate <NSObject>
@optional
- (void)headerDidClicked:(SLQHeader *)header;
@end
@interface SLQHeader : UITableViewHeaderFooterView
/**用户分组*/
@property (nonatomic, strong) SLQFriendGroup *friendGroup;
/** 按钮*/
@property (nonatomic, weak) UIButton *contentButton;
/** 标签*/
@property (nonatomic, weak) UILabel *onlineLabel;
/**delegate*/
@property (nonatomic, weak) id<SLQHeaderDelegate> delegate;
+ (instancetype)headerWithTableView:(UITableView *)tableView;
@end
- 5.6、在控制器里设置代理监听按钮点击即可
#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 1、自定义header
SLQHeader *header = [SLQHeader headerWithTableView:tableView];
// 2、传递模型
SLQFriendGroup *group = self.friendList[section];
header.friendGroup = group;
header.delegate = self;
// 3、返回头部
return header;
}
- (void)headerDidClicked:(SLQHeader *)header
{
// 刷新表格
[self.tableView reloadData];
}

- 这是俺的github
https://github.com/slq0378/05-QQ- - 还有一点就是原作者的那个工程有点问题,就是那个按钮图片的旋转不起作用,我这里直接把旋转代码放到
layoutSubviews,这样只要布局改变就会旋转箭头
iOS-QQ好友列表实现的更多相关文章
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- [iOS基础控件 - 6.9.3] QQ好友列表Demo TableView
A.需求 1.使用plist数据,展示类似QQ好友列表的分组.组内成员显示缩进功能 2.组名使用Header,展示箭头图标.组名.组内人数和上线人数 3.点击组名,伸展.缩回好友组 code so ...
- 仿QQ好友列表界面的实现
TableView有2种style:UITableViewStylePlain 和 UITableViewStyleGrouped. 但是QQ好友列表的tableView给人的感觉似乎是2个style ...
- ExpandableListView仿QQ好友列表
本例中,对ExpandableListView中的数据进行了封装,分为两个JavaBean,一个为Group类表示组信息,一个Child类表示该组下子列表信息: Group: public class ...
- (二十七)QQ好友列表的实现
QQ好友列表通过plist读取,plist的结构为一组字典,每个字典内有本组的信息和另外一组字典代表好友. 要读取plist,选择合适的数据结构,例如NSArray,然后调用initWithConte ...
- android 实现QQ好友列表
在某些Android开发群里,看到有些新手问怎么实现QQ好友列表,其实网上一搜挺多的.接触Android,也才一年的时间,大部分时间花在工作上(解bug...),界面上开发很少参与.自己维护的系统应用 ...
- 基于Qt的相似QQ好友列表抽屉效果的实现
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shuideyidi/article/details/30619167 前段时间在忙毕业设计, ...
- swift 实现QQ好友列表功能
最近项目中有类似QQ好友列表功能,整理了一下,话不多说,直接上代码 import UIKit class QQFriend: NSObject { var name: String? var intr ...
- Windows UIA自动化测试框架学习--获取qq好友列表
前段时间应公司要求开发一款针对现有WPF程序的自动化测试工具,在网上查资料找了一段时间,发现用来做自动化测试的框架还是比较多的,比如python的两个模块pywinauto和uiautomation, ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
一.实现效果 二.实现代码 1.数据模型部分 YYQQGroupModel.h文件 // // YYQQGroupModel.h // 02-QQ好友列表(基本数据的加载) / ...
随机推荐
- Arduino-舵机控制Servo
以前没有接触过硬件,因为把弄APM2.5不得不去接触arduino板.Arduino是块极易上手的控板,不像单片机,你要花费大量的时间去学习预备知识,它只要你稍微懂点C语言既能上手.对于我这种业余爱好 ...
- MySQL安装和简单操作
MySQL数据库安装与配置详解 MySQL的安装请参考下面这篇博客,讲述的非常详细,各种截图也很清晰.http://www.cnblogs.com/sshoub/p/4321640.html MySQ ...
- Uva 11078 简单dp
题目链接:http://uva.onlinejudge.org/external/110/11078.pdf a[i] - a[j] 的最大值. 这个题目马毅问了我,O(n^2)超时,记忆化一下当前最 ...
- 2295: KMP模式匹配 一(串)
2295: KMP模式匹配 一(串) 时间限制: 1 Sec 内存限制: 128 MB提交: 210 解决: 97[提交][状态][讨论版][命题人:外部导入] 题目描述 求子串的next值,用n ...
- 安卓装tensorflow
1.https://blog.csdn.net/masa_fish/article/details/54096996 tensorflow有很多中安装方式,但你要想和android结合,就只能采用源码 ...
- jQuery UI datepicker z-index默认为1 怎么处理
最近在维护一个后台系统的时候遇到这样的一个坑:后台系统中日期控件使用的是jQuery UI datepicker. 这个控件生成的日期选择框的z-index = 1.问题来了.页面上有不少z-inde ...
- js 实现序列号效果实现
前端的朋友可能遇到过这样的需求,要求在页面输入一串序列号,或激活码(就像在PC正版软件中的序列号),可是HTML中并没有为我们提供类似的组件,我们来自己实现一个: 大体的思路是在表单里有一个隐藏的in ...
- C# as运算符
一.C# as运算符 as运算符用于执行引用类型的显式类型转换.请阅读C#数据类型. as运算符可以用下面的格式表示: expression as type?expression:引用类型的表达式. ...
- wm_concat()函数
工作中遇到这样一个问题,一张数据库表中有一个字段file_id,还有一个主键f_id(唯一),而file_id不唯一,我想把file_id=‘123456789’的记录中的f_id(主键)连接成一个字 ...
- iOS中 XMPP即时通讯实现的主要步骤
这里只是列出实现的只要步骤,不是全部代码. 首先导入XMPPFramework,及相关配置,完成后开始. 创建一个XMPPHelper 类来管理要进行的操作. XMPPHelper.h文件如下 ty ...