IOS UI-QQ好友列表
一、Model
- //
- // FriendsModel.h
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface FriendsModel : NSObject
- @property (nonatomic, copy) NSString *icon;
- @property (nonatomic, copy) NSString *intro;
- @property (nonatomic, copy) NSString *name;
- @property (nonatomic, assign, getter=isVip) BOOL vip;
- - (instancetype)initWithDict:(NSDictionary *)dict;
- + (instancetype)friendsModelWithDict:(NSDictionary *)dict;
- @end
- //
- // FriendsModel.m
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import "FriendsModel.h"
- @implementation FriendsModel
- - (instancetype) initWithDict:(NSDictionary *)dict
- {
- if (self = [super init]) {
- [self setValuesForKeysWithDictionary:dict];
- }
- return self;
- }
- + (instancetype)friendsModelWithDict:(NSDictionary *)dict
- {
- return [[self alloc] initWithDict:dict];
- }
- @end
- //
- // GroupModel.h
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface GroupModel : NSObject
- @property (nonatomic, copy) NSString *name;
- @property (nonatomic, assign) NSInteger online;
- @property (nonatomic, strong) NSArray *friends;
- //设置组是否可见
- @property (nonatomic, assign, getter=isVisible) BOOL visible;
- - (instancetype)initWithDict:(NSDictionary *)dict;
- + (instancetype)groupWithDict:(NSDictionary *)dict;
- @end
- //
- // GroupModel.m
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import "GroupModel.h"
- #import "FriendsModel.h"
- @implementation GroupModel
- - (instancetype)initWithDict:(NSDictionary *)dict
- {
- if (self = [self init]) {
- [self setValuesForKeysWithDictionary:dict];
- // 创建一个可变数组用来存放当前组中所有好友的模型
- NSMutableArray *friendArr = [NSMutableArray array];
- // 遍历当前组中所有好友的字典数组,所字典数组转换成模型数组
- for (NSDictionary *dict in self.friends) {
- // 把当前的好友字典转换成好友模型
- FriendsModel *model = [FriendsModel friendsModelWithDict:dict];
- // 把好友模型添加到数组中
- [friendArr addObject:model];
- }
- // 把装有当前组所有好友的模型数组赋值给组模型的friends属性
- _friends =friendArr;
- }
- return self;
- }
- + (instancetype)groupWithDict:(NSDictionary *)dict
- {
- return [[self alloc] initWithDict:dict];
- }
- @end
二、View
- //
- // FriendTableViewCell.h
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @class FriendsModel;
- @interface FriendTableViewCell : UITableViewCell
- @property (nonatomic, strong) FriendsModel *friendModel;
- + (instancetype)friendCellWithTableView:(UITableView *)tableView;
- @end
- //
- // FriendTableViewCell.m
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import "FriendTableViewCell.h"
- #import "FriendsModel.h"
- @implementation FriendTableViewCell
- //创建单元格
- + (instancetype)friendCellWithTableView:(UITableView *)tableView
- {
- static NSString *cellIdentifier = @"cellIdentifier";
- FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- if (!cell) {
- cell = [[FriendTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
- }
- return cell;
- }
- //设置单元格数据
- - (void)setFriendModel:(FriendsModel *)friendModel
- {
- _friendModel = friendModel;
- //NSLog(@"%@",_friendModel.icon);
- //把模型数据设置给子控件
- self.imageView.image = [UIImage imageNamed:_friendModel.icon];
- self.textLabel.text = _friendModel.name;
- self.detailTextLabel.text = _friendModel.intro;
- //根据当前好友是不是VIP来决定是否将昵称显示为红色
- self.textLabel.textColor = _friendModel.vip ? [UIColor redColor] : [UIColor blackColor];
- }
- - (void)awakeFromNib {
- // Initialization code
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated];
- // Configure the view for the selected state
- }
- @end
- //
- // GroupHeaderView.h
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @class GroupHeaderView;
- @protocol GroupHeaderViewDelegate<NSObject>
- - (void)groupHeaderViewDidClickTitleButton:(GroupHeaderView *)groupHeaderView;
- @end
- @class GroupModel;
- @interface GroupHeaderView : UITableViewHeaderFooterView
- @property (nonatomic, strong) GroupModel *groupModel;
- //增加一个代理属性
- @property (nonatomic, weak) id<GroupHeaderViewDelegate> delegate;
- + (instancetype)groupHeaderViewWithTableView:(UITableView *)tableView;
- @end
- //
- // GroupHeaderView.m
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import "GroupHeaderView.h"
- #import "GroupModel.h"
- @interface GroupHeaderView ()
- @property (nonatomic, strong) UIButton *btnGroupTitle;
- @property (nonatomic, strong) UILabel *lblCount;
- @end
- @implementation GroupHeaderView
- #pragma mark - groupHeaderViewWithTableView方法
- //封装一个类方法,创建GroupHeaderView
- + (instancetype)groupHeaderViewWithTableView:(UITableView *)tableView
- {
- static NSString *ID = @"UITableViewHeaderFooterView";
- GroupHeaderView *HeaderView = [tableView dequeueReusableCellWithIdentifier:ID];
- if (HeaderView == nil) {
- HeaderView = [[GroupHeaderView alloc] initWithReuseIdentifier:ID];
- }
- return HeaderView;
- }
- #pragma mark - initWithReuseIdentifier方法
- //重写initWithReuseIdentifier:方法,在创建GroupHeaderView时,同时创建子控件
- - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
- {
- if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
- //创建按钮
- self.btnGroupTitle = [[UIButton alloc] init];
- //设置图片
- [self.btnGroupTitle setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
- //设置按钮背景图片
- [self.btnGroupTitle setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
- [self.btnGroupTitle setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
- //设置按钮中整体内容左对齐
- self.btnGroupTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
- //设置按钮内容的内边距
- self.btnGroupTitle.contentEdgeInsets = UIEdgeInsetsMake(, , , );
- //设置按钮标题距离左边的边距
- [self.btnGroupTitle setTitleEdgeInsets:UIEdgeInsetsMake(, , , )];
- //设置按钮中图片的显示模式
- self.btnGroupTitle.imageView.contentMode = UIViewContentModeCenter;
- //让图片框超出的部分不要截掉
- self.btnGroupTitle.imageView.clipsToBounds = NO;
- //点击事件
- [self.btnGroupTitle addTarget:self action:@selector(btnGroupTitleClick) forControlEvents:UIControlEventTouchUpInside];
- //加载到视图
- [self.contentView addSubview:self.btnGroupTitle];
- //创建Label
- self.lblCount = [[UILabel alloc] init];
- [self.contentView addSubview:self.lblCount];
- }
- return self;
- }
- //组标题按钮的点击事件
- - (void)btnGroupTitleClick
- {
- //1.设置组的状态
- self.groupModel.visible = !self.groupModel.isVisible;
- //2.刷新TableView的数据
- if (self.delegate != nil && [self.delegate respondsToSelector:@selector(groupHeaderViewDidClickTitleButton:)]) {
- //调用代理方法
- [self.delegate groupHeaderViewDidClickTitleButton:self];
- }
- }
- #pragma mark - didMoveToSuperview方法
- //当一个新的headerView已经加载到父控件中执行这个方法
- - (void)didMoveToSuperview
- {
- if (self.groupModel.visible ) {
- //让按钮中的图片旋转
- self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
- }
- else
- //让按钮中的图片旋转
- self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation();
- }
- #pragma mark - setGroupModel方法
- //重写setGroupModel方法
- - (void)setGroupModel:(GroupModel *)groupModel
- {
- _groupModel = groupModel;
- //设置数据
- //设置按钮上的文字
- [self.btnGroupTitle setTitle:_groupModel.name forState:UIControlStateNormal];
- [self.btnGroupTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- //设置Label上的文字
- self.lblCount.text = [NSString stringWithFormat:@"%lu / %lu",_groupModel.online,_groupModel.friends.count];
- //解决按钮中的图片旋转问题
- if (self.groupModel.visible ) {
- //让按钮中的图片旋转
- self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
- }
- else
- //让按钮中的图片旋转
- self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation();
- //不要在这里设置frame,因为当前控件的frame为0;
- }
- #pragma mark - layoutSubviews方法
- //当前控件的frame发生改变调用这个方法
- - (void)layoutSubviews
- {
- [super layoutSubviews];
- //设置frame
- self.btnGroupTitle.frame = self.bounds;
- CGFloat lblW = ;
- CGFloat lblH = self.bounds.size.height;
- CGFloat lblX = self.bounds.size.width - - lblW;
- CGFloat lblY = ;
- self.lblCount.frame = CGRectMake(lblX, lblY, lblW, lblH);
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
三、Controller
- //
- // QQFriendsTableVC.m
- // IOS_0111_好友列表
- //
- // Created by ma c on 16/1/11.
- // Copyright (c) 2016年 博文科技. All rights reserved.
- //
- #import "QQFriendsTableVC.h"
- #import "GroupModel.h"
- #import "FriendsModel.h"
- #import "FriendTableViewCell.h"
- #import "GroupHeaderView.h"
- @interface QQFriendsTableVC ()<GroupHeaderViewDelegate>
- @property (nonatomic, strong) NSArray *groupArr;
- @end
- @implementation QQFriendsTableVC
- #pragma mark - 懒加载
- - (NSArray *)groupArr
- {
- if (_groupArr == nil) {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
- NSArray *arrDict = [NSArray arrayWithContentsOfFile:path];
- NSMutableArray *arrModel = [NSMutableArray array];
- for (NSDictionary *dict in arrDict) {
- GroupModel *model = [GroupModel groupWithDict:dict];
- [arrModel addObject:model];
- }
- _groupArr =arrModel;
- }
- return _groupArr;
- }
- #pragma mark - 数据源方法
- //组数
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return self.groupArr.count;
- }
- //每组行数
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- GroupModel *model = self.groupArr[section];
- if (model.visible == NO) {
- return ;
- }
- else
- return model.friends.count;
- }
- //设置单元格
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //1.获取模型
- GroupModel *groupModel = self.groupArr[indexPath.section];
- FriendsModel *friendsModel = groupModel.friends[indexPath.row];
- //2.创建单元格
- FriendTableViewCell *cell = [FriendTableViewCell friendCellWithTableView:tableView];
- //3.设置单元格内容
- cell.friendModel = friendsModel;
- //4.返回单元格
- return cell;
- }
- ////设置每一组的组标题(只能设置字符串,但我们每一组还包含其他子控件)
- //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- //{
- // GroupModel *groupModel = self.groupArr[section];
- // return groupModel.name;
- //}
- //设置HeaderView
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
- {
- //不要创建一个UIView返回,因为无法重用
- //为了重用每个Header中的UIView,这里要返回UITableViewHeaderFooterView
- //1.获取模型
- GroupModel *groupModel = self.groupArr[section];
- //2.创建UITableViewHeaderFooterView
- GroupHeaderView *HeaderView = [GroupHeaderView groupHeaderViewWithTableView:tableView];
- //因为刚创建好的HeaderView没有设置frame,所以frame为0,但是运行以后我们看到都是有frame
- //原因UITableView要用到HeaderView,会根据一些设置动态为HeaderView的frame赋值
- //3.设置数据
- HeaderView.groupModel = groupModel;
- //设置组的标志
- HeaderView.tag = section;
- //设置代理
- HeaderView.delegate = self;
- //4.返回view
- return HeaderView;
- }
- #pragma mark - viewDidLoad方法
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
- //统一设置每组标题的高度
- self.tableView.sectionHeaderHeight = ;
- self.tableView.tableFooterView = [[UIView alloc] init];
- }
- #pragma mark - GroupHeaderViewDelegate代理方法
- - (void)groupHeaderViewDidClickTitleButton:(GroupHeaderView *)groupHeaderView
- {
- //刷新TableView
- // [self.tableView reloadData];
- //局部刷新
- NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:groupHeaderView.tag];
- [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
- }
- #pragma mark - 隐藏状态栏
- - (BOOL)prefersStatusBarHidden
- {
- return YES;
- }
- @end
IOS UI-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 ...
- Windows UIA自动化测试框架学习--获取qq好友列表
前段时间应公司要求开发一款针对现有WPF程序的自动化测试工具,在网上查资料找了一段时间,发现用来做自动化测试的框架还是比较多的,比如python的两个模块pywinauto和uiautomation, ...
- 仿QQ好友列表界面的实现
TableView有2种style:UITableViewStylePlain 和 UITableViewStyleGrouped. 但是QQ好友列表的tableView给人的感觉似乎是2个style ...
- IOS 实现QQ好友分组展开关闭功能
贴出核心代码 主要讲一下思路. - (void)nameBtnClick:(myButton *)sender { //获取当前点击的分组对应的section self.clickIndex = s ...
- 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 ...
随机推荐
- 容斥原理解决某个区间[1,n]闭区间与m互质数数量问题
首先贴出代码(闭区间[1,n]范围内和m互质的数) 代码: int solve(II n,II m){ vector<II>p; ;i*i<=m;i++){ ){ p.push_ba ...
- 以jar包的形式来使用前端的各种框架、组件。
springboot(二):web综合开发 - 纯洁的微笑博客 http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html ...
- Python爬虫基础(一)urllib2库的基本使用
爬虫也就是所谓的网络数据采集,是一种通过多种手段收集网络数据的方式,不光是通过与 API 交互(或者直接与浏览器交互)的方式.最常用的方法是写一个自动化程序向网络服务器请求数据(通常是用 HTML 表 ...
- (2.10)Mysql之SQL基础——约束及主键重复处理
(2.10)Mysql之SQL基础——约束及主键重复处理 关键词:mysql约束,批量插入数据主键冲突 [1]查看索引: show index from table_name; [2]查看有约束的列: ...
- C# 反双曲余弦函数
反双曲余弦函数的定义是: T1 = Math.Log(t + Math.Sqrt(t * t - 1)); 1. 叉乘(cross product),也叫向量的外积.向量积.顾名思义,求下来的结果是一 ...
- spring Security 得到认证用户名的方法
@Service("userService")public class UserServiceImpl implements UserService { @Overridepubl ...
- mono安装
linux上的DotNET,安装mono 当前,在Linux系统上架设ASP.NET网站.建设WEB应用工程项目已经在国内流行起来,而“Mono+Jexus”架构模式是Linux承载ASP.NET企业 ...
- EF Code First 学习笔记:表映射(转)
多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...
- Transactions and beyond it..
While data integrity is managed very effectively within a single database with row locking, deadlock ...
- 梅尔频率倒谱系数(MFCC) 学习笔记
最近学习音乐自动标注的过程中,看到了有关使用MFCC提取音频特征的内容,特地在网上找到资料,学习了一下相关内容.此笔记大部分内容摘自博文 http://blog.csdn.net/zouxy09/ar ...