一、Model

  1. //
  2. // FriendsModel.h
  3. // IOS_0111_好友列表
  4. //
  5. // Created by ma c on 16/1/11.
  6. // Copyright (c) 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface FriendsModel : NSObject
  12.  
  13. @property (nonatomic, copy) NSString *icon;
  14. @property (nonatomic, copy) NSString *intro;
  15. @property (nonatomic, copy) NSString *name;
  16. @property (nonatomic, assign, getter=isVip) BOOL vip;
  17.  
  18. - (instancetype)initWithDict:(NSDictionary *)dict;
  19. + (instancetype)friendsModelWithDict:(NSDictionary *)dict;
  20.  
  21. @end
  22.  
  23. //
  24. // FriendsModel.m
  25. // IOS_0111_好友列表
  26. //
  27. // Created by ma c on 16/1/11.
  28. // Copyright (c) 2016年 博文科技. All rights reserved.
  29. //
  30.  
  31. #import "FriendsModel.h"
  32.  
  33. @implementation FriendsModel
  34.  
  35. - (instancetype) initWithDict:(NSDictionary *)dict
  36. {
  37. if (self = [super init]) {
  38. [self setValuesForKeysWithDictionary:dict];
  39.  
  40. }
  41. return self;
  42. }
  43.  
  44. + (instancetype)friendsModelWithDict:(NSDictionary *)dict
  45. {
  46. return [[self alloc] initWithDict:dict];
  47. }
  48.  
  49. @end
  1. //
  2. // GroupModel.h
  3. // IOS_0111_好友列表
  4. //
  5. // Created by ma c on 16/1/11.
  6. // Copyright (c) 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface GroupModel : NSObject
  12.  
  13. @property (nonatomic, copy) NSString *name;
  14. @property (nonatomic, assign) NSInteger online;
  15. @property (nonatomic, strong) NSArray *friends;
  16. //设置组是否可见
  17. @property (nonatomic, assign, getter=isVisible) BOOL visible;
  18.  
  19. - (instancetype)initWithDict:(NSDictionary *)dict;
  20. + (instancetype)groupWithDict:(NSDictionary *)dict;
  21.  
  22. @end
  23.  
  24. //
  25. // GroupModel.m
  26. // IOS_0111_好友列表
  27. //
  28. // Created by ma c on 16/1/11.
  29. // Copyright (c) 2016年 博文科技. All rights reserved.
  30. //
  31.  
  32. #import "GroupModel.h"
  33. #import "FriendsModel.h"
  34.  
  35. @implementation GroupModel
  36.  
  37. - (instancetype)initWithDict:(NSDictionary *)dict
  38. {
  39. if (self = [self init]) {
  40. [self setValuesForKeysWithDictionary:dict];
  41.  
  42. // 创建一个可变数组用来存放当前组中所有好友的模型
  43. NSMutableArray *friendArr = [NSMutableArray array];
  44. // 遍历当前组中所有好友的字典数组,所字典数组转换成模型数组
  45. for (NSDictionary *dict in self.friends) {
  46. // 把当前的好友字典转换成好友模型
  47. FriendsModel *model = [FriendsModel friendsModelWithDict:dict];
  48. // 把好友模型添加到数组中
  49. [friendArr addObject:model];
  50. }
  51. // 把装有当前组所有好友的模型数组赋值给组模型的friends属性
  52. _friends =friendArr;
  53. }
  54. return self;
  55. }
  56.  
  57. + (instancetype)groupWithDict:(NSDictionary *)dict
  58. {
  59. return [[self alloc] initWithDict:dict];
  60. }
  61.  
  62. @end

二、View

  1. //
  2. // FriendTableViewCell.h
  3. // IOS_0111_好友列表
  4. //
  5. // Created by ma c on 16/1/11.
  6. // Copyright (c) 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. @class FriendsModel;
  11. @interface FriendTableViewCell : UITableViewCell
  12.  
  13. @property (nonatomic, strong) FriendsModel *friendModel;
  14.  
  15. + (instancetype)friendCellWithTableView:(UITableView *)tableView;
  16.  
  17. @end
  18.  
  19. //
  20. // FriendTableViewCell.m
  21. // IOS_0111_好友列表
  22. //
  23. // Created by ma c on 16/1/11.
  24. // Copyright (c) 2016年 博文科技. All rights reserved.
  25. //
  26.  
  27. #import "FriendTableViewCell.h"
  28. #import "FriendsModel.h"
  29.  
  30. @implementation FriendTableViewCell
  31.  
  32. //创建单元格
  33. + (instancetype)friendCellWithTableView:(UITableView *)tableView
  34. {
  35. static NSString *cellIdentifier = @"cellIdentifier";
  36. FriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  37. if (!cell) {
  38. cell = [[FriendTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
  39. }
  40. return cell;
  41. }
  42. //设置单元格数据
  43. - (void)setFriendModel:(FriendsModel *)friendModel
  44. {
  45. _friendModel = friendModel;
  46. //NSLog(@"%@",_friendModel.icon);
  47.  
  48. //把模型数据设置给子控件
  49. self.imageView.image = [UIImage imageNamed:_friendModel.icon];
  50. self.textLabel.text = _friendModel.name;
  51. self.detailTextLabel.text = _friendModel.intro;
  52.  
  53. //根据当前好友是不是VIP来决定是否将昵称显示为红色
  54. self.textLabel.textColor = _friendModel.vip ? [UIColor redColor] : [UIColor blackColor];
  55.  
  56. }
  57.  
  58. - (void)awakeFromNib {
  59. // Initialization code
  60. }
  61.  
  62. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  63. [super setSelected:selected animated:animated];
  64.  
  65. // Configure the view for the selected state
  66. }
  67.  
  68. @end
  1. //
  2. // GroupHeaderView.h
  3. // IOS_0111_好友列表
  4. //
  5. // Created by ma c on 16/1/11.
  6. // Copyright (c) 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @class GroupHeaderView;
  12. @protocol GroupHeaderViewDelegate<NSObject>
  13.  
  14. - (void)groupHeaderViewDidClickTitleButton:(GroupHeaderView *)groupHeaderView;
  15.  
  16. @end
  17.  
  18. @class GroupModel;
  19. @interface GroupHeaderView : UITableViewHeaderFooterView
  20.  
  21. @property (nonatomic, strong) GroupModel *groupModel;
  22.  
  23. //增加一个代理属性
  24. @property (nonatomic, weak) id<GroupHeaderViewDelegate> delegate;
  25.  
  26. + (instancetype)groupHeaderViewWithTableView:(UITableView *)tableView;
  27.  
  28. @end
  29.  
  30. //
  31. // GroupHeaderView.m
  32. // IOS_0111_好友列表
  33. //
  34. // Created by ma c on 16/1/11.
  35. // Copyright (c) 2016年 博文科技. All rights reserved.
  36. //
  37.  
  38. #import "GroupHeaderView.h"
  39. #import "GroupModel.h"
  40.  
  41. @interface GroupHeaderView ()
  42.  
  43. @property (nonatomic, strong) UIButton *btnGroupTitle;
  44. @property (nonatomic, strong) UILabel *lblCount;
  45.  
  46. @end
  47.  
  48. @implementation GroupHeaderView
  49.  
  50. #pragma mark - groupHeaderViewWithTableView方法
  51. //封装一个类方法,创建GroupHeaderView
  52. + (instancetype)groupHeaderViewWithTableView:(UITableView *)tableView
  53. {
  54. static NSString *ID = @"UITableViewHeaderFooterView";
  55. GroupHeaderView *HeaderView = [tableView dequeueReusableCellWithIdentifier:ID];
  56. if (HeaderView == nil) {
  57. HeaderView = [[GroupHeaderView alloc] initWithReuseIdentifier:ID];
  58. }
  59. return HeaderView;
  60. }
  61.  
  62. #pragma mark - initWithReuseIdentifier方法
  63. //重写initWithReuseIdentifier:方法,在创建GroupHeaderView时,同时创建子控件
  64. - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
  65. {
  66. if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
  67. //创建按钮
  68. self.btnGroupTitle = [[UIButton alloc] init];
  69. //设置图片
  70. [self.btnGroupTitle setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
  71. //设置按钮背景图片
  72. [self.btnGroupTitle setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
  73. [self.btnGroupTitle setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
  74. //设置按钮中整体内容左对齐
  75. self.btnGroupTitle.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  76. //设置按钮内容的内边距
  77. self.btnGroupTitle.contentEdgeInsets = UIEdgeInsetsMake(, , , );
  78. //设置按钮标题距离左边的边距
  79. [self.btnGroupTitle setTitleEdgeInsets:UIEdgeInsetsMake(, , , )];
  80. //设置按钮中图片的显示模式
  81. self.btnGroupTitle.imageView.contentMode = UIViewContentModeCenter;
  82. //让图片框超出的部分不要截掉
  83. self.btnGroupTitle.imageView.clipsToBounds = NO;
  84. //点击事件
  85. [self.btnGroupTitle addTarget:self action:@selector(btnGroupTitleClick) forControlEvents:UIControlEventTouchUpInside];
  86. //加载到视图
  87. [self.contentView addSubview:self.btnGroupTitle];
  88.  
  89. //创建Label
  90. self.lblCount = [[UILabel alloc] init];
  91. [self.contentView addSubview:self.lblCount];
  92. }
  93. return self;
  94. }
  95. //组标题按钮的点击事件
  96. - (void)btnGroupTitleClick
  97. {
  98. //1.设置组的状态
  99. self.groupModel.visible = !self.groupModel.isVisible;
  100. //2.刷新TableView的数据
  101. if (self.delegate != nil && [self.delegate respondsToSelector:@selector(groupHeaderViewDidClickTitleButton:)]) {
  102. //调用代理方法
  103. [self.delegate groupHeaderViewDidClickTitleButton:self];
  104. }
  105. }
  106. #pragma mark - didMoveToSuperview方法
  107. //当一个新的headerView已经加载到父控件中执行这个方法
  108. - (void)didMoveToSuperview
  109. {
  110. if (self.groupModel.visible ) {
  111. //让按钮中的图片旋转
  112. self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
  113. }
  114. else
  115. //让按钮中的图片旋转
  116. self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation();
  117. }
  118. #pragma mark - setGroupModel方法
  119. //重写setGroupModel方法
  120. - (void)setGroupModel:(GroupModel *)groupModel
  121. {
  122. _groupModel = groupModel;
  123. //设置数据
  124.  
  125. //设置按钮上的文字
  126. [self.btnGroupTitle setTitle:_groupModel.name forState:UIControlStateNormal];
  127. [self.btnGroupTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  128.  
  129. //设置Label上的文字
  130. self.lblCount.text = [NSString stringWithFormat:@"%lu / %lu",_groupModel.online,_groupModel.friends.count];
  131.  
  132. //解决按钮中的图片旋转问题
  133. if (self.groupModel.visible ) {
  134. //让按钮中的图片旋转
  135. self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
  136. }
  137. else
  138. //让按钮中的图片旋转
  139. self.btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation();
  140.  
  141. //不要在这里设置frame,因为当前控件的frame为0;
  142.  
  143. }
  144. #pragma mark - layoutSubviews方法
  145. //当前控件的frame发生改变调用这个方法
  146. - (void)layoutSubviews
  147. {
  148. [super layoutSubviews];
  149.  
  150. //设置frame
  151.  
  152. self.btnGroupTitle.frame = self.bounds;
  153.  
  154. CGFloat lblW = ;
  155. CGFloat lblH = self.bounds.size.height;
  156. CGFloat lblX = self.bounds.size.width - - lblW;
  157. CGFloat lblY = ;
  158. self.lblCount.frame = CGRectMake(lblX, lblY, lblW, lblH);
  159.  
  160. }
  161.  
  162. /*
  163. // Only override drawRect: if you perform custom drawing.
  164. // An empty implementation adversely affects performance during animation.
  165. - (void)drawRect:(CGRect)rect {
  166. // Drawing code
  167. }
  168. */
  169.  
  170. @end

三、Controller

  1. //
  2. // QQFriendsTableVC.m
  3. // IOS_0111_好友列表
  4. //
  5. // Created by ma c on 16/1/11.
  6. // Copyright (c) 2016年 博文科技. All rights reserved.
  7. //
  8.  
  9. #import "QQFriendsTableVC.h"
  10. #import "GroupModel.h"
  11. #import "FriendsModel.h"
  12. #import "FriendTableViewCell.h"
  13. #import "GroupHeaderView.h"
  14.  
  15. @interface QQFriendsTableVC ()<GroupHeaderViewDelegate>
  16.  
  17. @property (nonatomic, strong) NSArray *groupArr;
  18.  
  19. @end
  20.  
  21. @implementation QQFriendsTableVC
  22.  
  23. #pragma mark - 懒加载
  24. - (NSArray *)groupArr
  25. {
  26. if (_groupArr == nil) {
  27. NSString *path = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
  28. NSArray *arrDict = [NSArray arrayWithContentsOfFile:path];
  29.  
  30. NSMutableArray *arrModel = [NSMutableArray array];
  31. for (NSDictionary *dict in arrDict) {
  32.  
  33. GroupModel *model = [GroupModel groupWithDict:dict];
  34.  
  35. [arrModel addObject:model];
  36.  
  37. }
  38. _groupArr =arrModel;
  39. }
  40. return _groupArr;
  41.  
  42. }
  43. #pragma mark - 数据源方法
  44. //组数
  45. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  46. {
  47. return self.groupArr.count;
  48. }
  49. //每组行数
  50. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  51. {
  52. GroupModel *model = self.groupArr[section];
  53. if (model.visible == NO) {
  54. return ;
  55. }
  56. else
  57. return model.friends.count;
  58. }
  59. //设置单元格
  60. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  61. {
  62. //1.获取模型
  63. GroupModel *groupModel = self.groupArr[indexPath.section];
  64. FriendsModel *friendsModel = groupModel.friends[indexPath.row];
  65.  
  66. //2.创建单元格
  67. FriendTableViewCell *cell = [FriendTableViewCell friendCellWithTableView:tableView];
  68.  
  69. //3.设置单元格内容
  70. cell.friendModel = friendsModel;
  71.  
  72. //4.返回单元格
  73. return cell;
  74. }
  75. ////设置每一组的组标题(只能设置字符串,但我们每一组还包含其他子控件)
  76. //- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  77. //{
  78. // GroupModel *groupModel = self.groupArr[section];
  79. // return groupModel.name;
  80. //}
  81.  
  82. //设置HeaderView
  83. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  84. {
  85. //不要创建一个UIView返回,因为无法重用
  86. //为了重用每个Header中的UIView,这里要返回UITableViewHeaderFooterView
  87.  
  88. //1.获取模型
  89. GroupModel *groupModel = self.groupArr[section];
  90. //2.创建UITableViewHeaderFooterView
  91. GroupHeaderView *HeaderView = [GroupHeaderView groupHeaderViewWithTableView:tableView];
  92. //因为刚创建好的HeaderView没有设置frame,所以frame为0,但是运行以后我们看到都是有frame
  93. //原因UITableView要用到HeaderView,会根据一些设置动态为HeaderView的frame赋值
  94. //3.设置数据
  95. HeaderView.groupModel = groupModel;
  96. //设置组的标志
  97. HeaderView.tag = section;
  98. //设置代理
  99. HeaderView.delegate = self;
  100. //4.返回view
  101. return HeaderView;
  102.  
  103. }
  104. #pragma mark - viewDidLoad方法
  105. - (void)viewDidLoad
  106. {
  107. [super viewDidLoad];
  108. self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
  109. //统一设置每组标题的高度
  110. self.tableView.sectionHeaderHeight = ;
  111. self.tableView.tableFooterView = [[UIView alloc] init];
  112. }
  113. #pragma mark - GroupHeaderViewDelegate代理方法
  114. - (void)groupHeaderViewDidClickTitleButton:(GroupHeaderView *)groupHeaderView
  115. {
  116. //刷新TableView
  117. // [self.tableView reloadData];
  118. //局部刷新
  119. NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:groupHeaderView.tag];
  120. [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
  121. }
  122.  
  123. #pragma mark - 隐藏状态栏
  124. - (BOOL)prefersStatusBarHidden
  125. {
  126. return YES;
  127. }
  128.  
  129. @end

IOS UI-QQ好友列表的更多相关文章

  1. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  2. [iOS基础控件 - 6.9.3] QQ好友列表Demo TableView

    A.需求 1.使用plist数据,展示类似QQ好友列表的分组.组内成员显示缩进功能 2.组名使用Header,展示箭头图标.组名.组内人数和上线人数 3.点击组名,伸展.缩回好友组   code so ...

  3. Windows UIA自动化测试框架学习--获取qq好友列表

    前段时间应公司要求开发一款针对现有WPF程序的自动化测试工具,在网上查资料找了一段时间,发现用来做自动化测试的框架还是比较多的,比如python的两个模块pywinauto和uiautomation, ...

  4. 仿QQ好友列表界面的实现

    TableView有2种style:UITableViewStylePlain 和 UITableViewStyleGrouped. 但是QQ好友列表的tableView给人的感觉似乎是2个style ...

  5. IOS 实现QQ好友分组展开关闭功能

    贴出核心代码  主要讲一下思路. - (void)nameBtnClick:(myButton *)sender { //获取当前点击的分组对应的section self.clickIndex = s ...

  6. ExpandableListView仿QQ好友列表

    本例中,对ExpandableListView中的数据进行了封装,分为两个JavaBean,一个为Group类表示组信息,一个Child类表示该组下子列表信息: Group: public class ...

  7. (二十七)QQ好友列表的实现

    QQ好友列表通过plist读取,plist的结构为一组字典,每个字典内有本组的信息和另外一组字典代表好友. 要读取plist,选择合适的数据结构,例如NSArray,然后调用initWithConte ...

  8. android 实现QQ好友列表

    在某些Android开发群里,看到有些新手问怎么实现QQ好友列表,其实网上一搜挺多的.接触Android,也才一年的时间,大部分时间花在工作上(解bug...),界面上开发很少参与.自己维护的系统应用 ...

  9. 基于Qt的相似QQ好友列表抽屉效果的实现

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shuideyidi/article/details/30619167     前段时间在忙毕业设计, ...

  10. swift 实现QQ好友列表功能

    最近项目中有类似QQ好友列表功能,整理了一下,话不多说,直接上代码 import UIKit class QQFriend: NSObject { var name: String? var intr ...

随机推荐

  1. 容斥原理解决某个区间[1,n]闭区间与m互质数数量问题

    首先贴出代码(闭区间[1,n]范围内和m互质的数) 代码: int solve(II n,II m){ vector<II>p; ;i*i<=m;i++){ ){ p.push_ba ...

  2. 以jar包的形式来使用前端的各种框架、组件。

    springboot(二):web综合开发 - 纯洁的微笑博客 http://www.ityouknow.com/springboot/2016/02/03/spring-boot-web.html ...

  3. Python爬虫基础(一)urllib2库的基本使用

    爬虫也就是所谓的网络数据采集,是一种通过多种手段收集网络数据的方式,不光是通过与 API 交互(或者直接与浏览器交互)的方式.最常用的方法是写一个自动化程序向网络服务器请求数据(通常是用 HTML 表 ...

  4. (2.10)Mysql之SQL基础——约束及主键重复处理

    (2.10)Mysql之SQL基础——约束及主键重复处理 关键词:mysql约束,批量插入数据主键冲突 [1]查看索引: show index from table_name; [2]查看有约束的列: ...

  5. C# 反双曲余弦函数

    反双曲余弦函数的定义是: T1 = Math.Log(t + Math.Sqrt(t * t - 1)); 1. 叉乘(cross product),也叫向量的外积.向量积.顾名思义,求下来的结果是一 ...

  6. spring Security 得到认证用户名的方法

    @Service("userService")public class UserServiceImpl implements UserService { @Overridepubl ...

  7. mono安装

    linux上的DotNET,安装mono 当前,在Linux系统上架设ASP.NET网站.建设WEB应用工程项目已经在国内流行起来,而“Mono+Jexus”架构模式是Linux承载ASP.NET企业 ...

  8. EF Code First 学习笔记:表映射(转)

      多个实体映射到一张表 Code First允许将多个实体映射到同一张表上,实体必须遵循如下规则: 实体必须是一对一关系 实体必须共享一个公共键 观察下面两个实体: public class Per ...

  9. Transactions and beyond it..

    While data integrity is managed very effectively within a single database with row locking, deadlock ...

  10. 梅尔频率倒谱系数(MFCC) 学习笔记

    最近学习音乐自动标注的过程中,看到了有关使用MFCC提取音频特征的内容,特地在网上找到资料,学习了一下相关内容.此笔记大部分内容摘自博文 http://blog.csdn.net/zouxy09/ar ...