AJ学IOS(19)UI之QQ好友列表
AJ分享,必须精品
先看效果图
哈哈,这次猫猫给来个动态的图片,这个看起来带劲
实现思路
首先建立模型
这里用到的是一个双层的模型。
cell的实现
这里一看其实就知道是一个tableView,我们自定义cell实现细节
headerView的实现
每一组的标题头其实都是headerVIew这里都是按钮需要我们自己设计。
代码实现
双层模型的代码
FriendCell:
#import <Foundation/Foundation.h>
#import "NJGlobal.h"
@interface NJFriendModel : NSObject
// 头像
@property (nonatomic, copy) NSString *icon;
// 简介
@property (nonatomic, copy) NSString *intro;
// 昵称
@property (nonatomic, copy) NSString *name;
// 是否是vip
@property (nonatomic, assign, getter = isVip) BOOL vip;
NJInitH(friend)
@end
QQGroupModel:
#import <Foundation/Foundation.h>
#import "NJGlobal.h"
@interface NJQQGroupModel : NSObject
/// 存放当前组所有的好友信息(好友模型)
@property (nonatomic, strong) NSArray *friends;
// 分组名称
@property (nonatomic, copy) NSString *name;
// 在线人数
@property (nonatomic, copy) NSString *online;
// 记录当前组是否需要打开
@property (nonatomic, assign, getter = isOpen) BOOL open;
NJInitH(qqGroup)
@end
@implementation NJQQGroupModel
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
// 1.将字典转换成模型
[self setValuesForKeysWithDictionary:dict];
// 定义数组保存转换后的模型
NSMutableArray *models = [NSMutableArray arrayWithCapacity:self.friends.count];
// 2.特殊处理friends中的数据
for (NSDictionary *dict in self.friends) {
// 2.1转换为模型
NJFriendModel *friend = [NJFriendModel friendWithDict:dict];
[models addObject:friend];
}
self.friends = models;
}
return self;
}
+ (instancetype)qqGroupWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
cell 的自定义
#import <UIKit/UIKit.h>
@class NJFriendModel;
@interface NJFriendCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@property (nonatomic, strong) NJFriendModel *friendData;
@end
@implementation NJFriendCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *identifier = @"friend";
NJFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[NJFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
return cell;
}
- (void)setFriendData:(NJFriendModel *)friendData
{
_friendData = friendData;
// 1.设置头像
self.imageView.image = [UIImage imageNamed:_friendData.icon];
// 2.设置昵称
self.textLabel.text = _friendData.name;
// 3.设置简介
self.detailTextLabel.text = _friendData.intro;
// 4.判断是否是会员
if (_friendData.isVip) {
[self.textLabel setTextColor:[UIColor redColor]];
}else
{
[self.textLabel setTextColor:[UIColor blackColor]];
}
}
@end
header的自定义
#import "NJHeaderView.h"
#import "NJQQGroupModel.h"
#import <UIKit/UIKit.h>
@class NJFriendModel;
@interface NJFriendCell : UITableViewCell
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@property (nonatomic, strong) NJFriendModel *friendData;
@end
@implementation NJFriendCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *identifier = @"friend";
NJFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[NJFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
return cell;
}
- (void)setFriendData:(NJFriendModel *)friendData
{
_friendData = friendData;
// 1.设置头像
self.imageView.image = [UIImage imageNamed:_friendData.icon];
// 2.设置昵称
self.textLabel.text = _friendData.name;
// 3.设置简介
self.detailTextLabel.text = _friendData.intro;
// 4.判断是否是会员
if (_friendData.isVip) {
[self.textLabel setTextColor:[UIColor redColor]];
}else
{
[self.textLabel setTextColor:[UIColor blackColor]];
}
}
@end
@interface NJHeaderView ()
@property (nonatomic, weak) UIButton *btn;
@property (nonatomic, weak) UILabel *label;
@end
@implementation NJHeaderView
// 创建头部视图
+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
// 1.创建头部视图
static NSString *identifier = @"header";
NJHeaderView *headderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];
if (headderView == nil) {
headderView = [[NJHeaderView alloc] initWithReuseIdentifier:identifier];
}
return headderView;
}
// 但凡在init方法中获取到的frame都是0
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
// 1.添加子控件
// 1.1添加按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 监听按钮的点击事件
[btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
// 设置按钮的背景图片
[btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
// 设置按钮上的尖尖图片
[btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
// 1.设置按钮的内容左对齐
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 2.设置按钮的内边距,然按钮的内容距离左边有一定的距离
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
// 3.设置按钮的标题和图片之间的距离
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
// btn.imageEdgeInsets
// 设置按钮标题颜色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 设置btn中的图片不填充整个imageview
btn.imageView.contentMode = UIViewContentModeCenter;
// 超出范围的图片不要剪切
// btn.imageView.clipsToBounds = NO;
btn.imageView.layer.masksToBounds = NO;
[self addSubview:btn];
self.btn = btn;
// 1.2添加label
UILabel *label = [[UILabel alloc] init];
// label.backgroundColor = [UIColor greenColor];
// 设置文本右对齐
label.textAlignment = NSTextAlignmentRight;
label.textColor = [UIColor grayColor];
[self addSubview:label];
self.label = label;
}
// NSLog(@"initWithReuseIdentifier = %@", NSStringFromCGRect(self.frame));
return self;
}
// 该方法在控件的frame被改变的时候就会调用
// 该方法一般用于调整子控件的位置
- (void)layoutSubviews
{
#warning 切记重写layoutSubviews方法一定要调用父类的layoutSubviews
[super layoutSubviews];
// 1.设置按钮的frame
self.btn.frame = self.bounds;
// 2.设置label的frame
CGFloat padding = 20;// 间隙
CGFloat labelY = 0;
CGFloat labelH = self.bounds.size.height;
CGFloat labelW = 150;
CGFloat labelX = self.bounds.size.width - padding - labelW;
self.label.frame = CGRectMake(labelX, labelY, labelW, labelH);
}
- (void)btnOnClick:(UIButton *)btn
{
NSLog(@"按钮被点击了");
// 1.修改组模型的isOpen属性
// 修改模型数据数据
self.qqGroup.open = !self.qqGroup.isOpen;
// 2. 刷新表格(通知代理)
if ([self.delegate respondsToSelector:@selector(headerViewDidClickHeaderView:)]) {
[self.delegate headerViewDidClickHeaderView:self];
}
// 3.修改btn上图片,让图片旋转
// self.btn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
// NSLog(@"%p %@", self, self.qqGroup.name);
}
#pragma mark - 当一个控件被添加到其它视图上的时候会调用以下方法
// 已经被添加到父视图上的时候会调用
- (void)didMoveToSuperview
{
// 在这个方法中就快要拿到最新的被添加到tableview上的头部视图修改它的图片
if (self.qqGroup.isOpen) {
self.btn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
}
}
// 即将被添加到父视图上的时候会调用
- (void)willMoveToSuperview:(UIView *)newSuperview
{
// NSLog(@"willMoveToSuperview");
}
- (void)setQqGroup:(NJQQGroupModel *)qqGroup
{
_qqGroup = qqGroup;
// 1.设置按钮上的文字
[self.btn setTitle:_qqGroup.name forState:UIControlStateNormal];
// 2.设置在线人数
self.label.text = [NSString stringWithFormat:@"%@/%d", _qqGroup.online, _qqGroup.friends.count];
}
@end
控制器代码
#import "NJViewController.h"
#import "NJQQGroupModel.h"
#import "NJFriendModel.h"
#import "NJFriendCell.h"
#import "NJHeaderView.h"
@interface NJViewController ()<NJHeaderViewDelegate>
// 保存所有的分组数据
@property (nonatomic, strong) NSArray *qqGroups;
@end
@implementation NJViewController
#pragma mark - 懒加载
- (NSArray *)qqGroups
{
if (_qqGroups == nil) {
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
NJQQGroupModel *qqGroip = [NJQQGroupModel qqGroupWithDict:dict];
[models addObject:qqGroip];
}
self.qqGroups = [models copy];
}
return _qqGroups;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.qqGroups.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 1.取出对应的组模型
NJQQGroupModel *qqGroup = self.qqGroups[section];
// 2.返回对应组中的好友数
// return qqGroup.friends.count;
// return 0;
if (qqGroup.isOpen) {
// 代表要展开
return qqGroup.friends.count;
}else
{
// 代表要合拢
return 0;
}
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
NJFriendCell *cell = [NJFriendCell cellWithTableView:tableView];
// 2.传递模型
// 2.1.取出对应的组模型
NJQQGroupModel *qqGroup = self.qqGroups[indexPath.section];
NJFriendModel *friend = qqGroup.friends[indexPath.row];
cell.friendData = friend;
// 3.返回cell
return cell;
}
#pragma mark - NJHeaderViewDelegate
- (void)headerViewDidClickHeaderView:(NJHeaderView *)headerView
{
// 重新调用数据源的方法加载数据
[self.tableView reloadData];
}
#pragma mark - 代理方法
// 当一个分组标题进入视野的时候就会调用该方法
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// 1.创建头部视图, 头部视图有默认的宽高,是系统自动设置
NJHeaderView *headerView = [NJHeaderView headerViewWithTableView:tableView];
// 设置当前控制器为代理
headerView.delegate = self;
// 2.传递模型
NJQQGroupModel *qqGroup = self.qqGroups[section];
// 0 - 0x8fa7ef0
headerView.qqGroup = qqGroup;
NSLog(@"%d - %p", section, headerView);
// 3.返回头部视图
return headerView;
}
// 设置分组头部标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44;
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
@end
AJ学IOS(19)UI之QQ好友列表的更多相关文章
- AJ学IOS 之ipad开发qq空间项目横竖屏幕适配
AJ分享,必须精品 一:效果图 先看效果 二:结构图 如图所示: 其中用到了UIView+extension分类 Masonry第三方框架做子控制器的适配 NYHomeViewController对应 ...
- iOS UITableView制作类似QQ好友列表视图
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDele ...
- 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 ...
- android 实现QQ好友列表
在某些Android开发群里,看到有些新手问怎么实现QQ好友列表,其实网上一搜挺多的.接触Android,也才一年的时间,大部分时间花在工作上(解bug...),界面上开发很少参与.自己维护的系统应用 ...
- Windows UIA自动化测试框架学习--获取qq好友列表
前段时间应公司要求开发一款针对现有WPF程序的自动化测试工具,在网上查资料找了一段时间,发现用来做自动化测试的框架还是比较多的,比如python的两个模块pywinauto和uiautomation, ...
- 仿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 ...
随机推荐
- Uncovering thousands of new peptides with sequence-mask-search hybrid de novo peptide sequencing framework (使用序列掩码搜索结合肽段从头测序框架发现了数千个新肽段)-解读人:刘佳维
期刊名:Molecular & Cellular Proteomics 发表时间:(2019年12月) IF:4.828 单位: 朱拉隆功大学 费城威斯塔研究所 物种:人 技术:de novo ...
- Happens-before先行发生原则
简介 从JDK1.5,java使用新的JSR-133内存模型:JSR-133使用happens-before的概念来阐述操作之间的内存可见性:在JMM中,如果一个操作执行的结果需要对另一个操作可见,那 ...
- python基本数据类型及其使用方法
前言 python中的数据类型主要为int.float.string.list.tuple.dict.set.bool.bytes.接下来int和float统称为数字类型. 1.数据类型总结 按存 ...
- 初识socket之TCP协议
TCP服务端.客户端(基础版本) # 这是服务端import socket server = socket.socket() # 买手机server.bind(('127.0.0.1', 8080 ...
- Django 模型笔记
关于模型: 1:一个模型类对应一个表,模型类中的属性对应表中的一个字段 2:字段类型(数据库支持的类型) 模型属性 字符串 1:CharField(Maxlength=长度) models.CharF ...
- [Flink] Flink的waterMark的通俗理解
导读 Flink 为实时计算提供了三种时间,即事件时间(event time).摄入时间(ingestion time)和处理时间(processing time). 遇到的问题: 假设在一个5秒的T ...
- 【笔记3-31】Python语言基础-序列sequence
序列sequence 可变序列 列表 list 字典 不可变序列 字符串 str 元祖 tuple 通过索引修改列表 del 删除元素 del my_list[2] 切片赋值只能是序列 .insert ...
- 构建安全可靠的微服务 | Nacos 在颜铺 SaaS 平台的应用实践
作者 | 殷铭 颜铺科技架构师 本文整理自架构师成长系列 3 月 19 日直播课程. 关注"阿里巴巴云原生"公众号,回复 "319",即可获取对应直播回放链接 ...
- “GANs”与“ODEs”:数学建模的终结?
在本文中,我想将经典数学建模和机器学习之间建立联系,它们以完全不同的方式模拟身边的对象和过程.虽然数学家基于他们的专业知识和对世界的理解来创建模型,而机器学习算法以某种隐蔽的不完全理解的方式描述世界, ...
- arcgis10.4.X的oracle数据库要求
受支持的数据库版本:(标准版/标准独立版/企业版) Oracle 11g R2(64 位)11.2.0.4 Oracle 12c R1(64 位)12.1.0.2 受支持的操作系统: 数据库 支持的操 ...