*****HMViewController.m

#import "HMViewController.h"
#import "HMFriendsGroupModel.h"
#import "HMFriendsModel.h"
#import "HMHeaderView.h"
@interface HMViewController ()<HMHeaderViewDelegate> //存放fiend内容
@property (nonatomic, strong)NSArray *friendsArr; @end @implementation HMViewController /**
* 当一个控件没有显示出来的时候
1. 父控件的frame
2. 当前控件frame
3. 当前控件 hidden (是否为yes )
4. alpha < = 0.01 */ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 当 cell header footer 是固定值的时候 使用以下操作
//frame 是动态的时候 不便调用
// self.tableView.rowHeight = 40;
// self.tableView.sectionHeaderHeight = 44;
// self.tableView.sectionFooterHeight = 44; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiClcik) name:@"friend" object:nil]; } - (void)notiClcik
{
[self.tableView reloadData];
} // 这个方法是在控制器销毁的时候调用
- (void)dealloc
{
//非ARC 必须调取这个方法
// [super dealloc]; [[NSNotificationCenter defaultCenter] removeObserver:self];
} - (NSArray *)friendsArr
{
if (_friendsArr == nil) {
NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"friends.plist" ofType:nil]]; NSMutableArray *friendsArray = [NSMutableArray array]; for (NSDictionary *dict in arr) {
HMFriendsGroupModel *model = [HMFriendsGroupModel friendsGroupWithDict:dict]; [friendsArray addObject:model]; } _friendsArr = friendsArray; } return _friendsArr;
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} //隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
} /**
* 设置header 的tiltle
*
* @return header 头部显示内容
*/
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//
// HMFriendsGroupModel *group = self.friendsArr[section];
//
// return group.name;
//} /**
* 设置headerVIew 的高度
*
*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return ;
} #pragma mark - header的代理方法
//- (void)headerView:(HMHeaderView *)view
//{
// [self.tableView reloadData];
//} #pragma mark - 数据源方法 多少组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//取出当前section 的model
HMFriendsGroupModel *model = self.friendsArr[section]; return model.open ? model.friends.count:;
} // 返回一个header 给tableview 显示
// 所有UIView 当只是做了init 操作的时候 然后做一些 关于frame 的操作, 这个frame的值肯定 没有值的
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{ HMHeaderView *header = [HMHeaderView headerViewWith:tableView]; HMFriendsGroupModel *model = self.friendsArr[section]; //让控制器充当headerView 的代理
// header.delegate = self; // sender 传过来的参数 ^ 是不block 象征 {};做相应操作
// header.block = ^(id sender){
// [tableView reloadData];
//
// NSLog(@"------%@",sender);
//
// }; header.group = model; // return [UIButton buttonWithType:UIButtonTypeContactAdd]; return header;
} //tableview 只中显示多少组 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.friendsArr.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"friends"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} //1 取出当前section model
HMFriendsGroupModel *group = self.friendsArr[indexPath.section]; //2 取出当前section model 中的 row 行
HMFriendsModel *model = group.friends[indexPath.row]; cell.textLabel.text = model.name;
//设置cell text 的颜色
cell.textLabel.textColor = model.vip?[UIColor redColor]:[UIColor blackColor];
cell.imageView.image = [UIImage imageNamed:model.icon]; return cell;
} @end
#import <UIKit/UIKit.h>

@interface HMViewController : UITableViewController

@end

******model HMFriendsGroupModel.h

#import <Foundation/Foundation.h>

@interface HMFriendsGroupModel : NSObject

//友源函数 friend 是个c++的关键字

/**
* 存放每行显示的内容
*/
@property (nonatomic, strong)NSArray *friends; /**
* headerView 显示的内容
*/
@property (nonatomic, copy)NSString *name; /**
* 在线人数
*/
@property (nonatomic, assign)int online;
/**
* 展开的时候
*/
@property (nonatomic, assign)BOOL open; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)friendsGroupWithDict:(NSDictionary *)dict; @end

******model HMFriendsGroupModel.m

#import "HMFriendsGroupModel.h"
#import "HMFriendsModel.h"
@implementation HMFriendsGroupModel - (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
//注入所有值
[self setValuesForKeysWithDictionary:dict]; NSMutableArray *arr = [NSMutableArray array];
for (NSDictionary *dict in self.friends) {
HMFriendsModel *model = [HMFriendsModel friendsWithDict:dict]; [arr addObject:model]; } self.friends = arr; } return self;
} + (instancetype)friendsGroupWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
} @end

***HMFriendsModel.h

#import <Foundation/Foundation.h>

@interface HMFriendsModel : NSObject

/**
* 名称
*/
@property (nonatomic, copy)NSString *name; /**
* 用户图片
*/
@property (nonatomic, copy)NSString *icon;; /**
* 个性签名
*/
@property (nonatomic, copy)NSString *intro;; /**
* 是否是vip
*/
@property (nonatomic, assign,getter = isVip)BOOL vip; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)friendsWithDict:(NSDictionary *)dict; @end

***HMFriendsModel.m

#import "HMFriendsModel.h"

@implementation HMFriendsModel

- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) { [self setValuesForKeysWithDictionary:dict];
} return self; } + (instancetype)friendsWithDict:(NSDictionary *)dict
{
return [[self alloc]initWithDict:dict];
} @end

**HMHeaderView.h

#import <UIKit/UIKit.h>
@class HMHeaderView; typedef void(^HMHeaderViewBlock)(id); //biock @protocol HMHeaderViewDelegate <NSObject> //代理 @optional
- (void)headerView:(HMHeaderView *)view; @end @class HMFriendsGroupModel; @interface HMHeaderView : UITableViewHeaderFooterView + (instancetype)headerViewWith:(UITableView *)tableview; @property (nonatomic, assign)id<HMHeaderViewDelegate> delegate; @property (nonatomic, strong)HMFriendsGroupModel *group; @property (nonatomic, copy)HMHeaderViewBlock block; @end

**HMHeaderView.m

#import "HMHeaderView.h"
#import "HMFriendsGroupModel.h"
@interface HMHeaderView () @property (nonatomic, weak)UIButton *nameBtn; @property (nonatomic, weak)UILabel *textLbl; @end @implementation HMHeaderView + (instancetype)headerViewWith:(UITableView *)tableview
{ static NSString *ID = @"headerView";
//首先看缓存池中是否存在headerView,如果存在的 直接取出来用
HMHeaderView *header = [tableview dequeueReusableHeaderFooterViewWithIdentifier:ID]; if (header == nil) {
//如果不存在 重新创建一个
header = [[HMHeaderView alloc]initWithReuseIdentifier:ID];
} return header;
} - (void)setGroup:(HMFriendsGroupModel *)group
{ //1. 必须做的操作
_group = group; [self.nameBtn setTitle:group.name forState:UIControlStateNormal]; //显示在线人数
self.textLbl.text = [NSString stringWithFormat:@"%d/%d",group.online,group.friends.count]; } // 当headerview 上子控件只需 做一次操作的 或者 要显示出来的 就写在以下方法中
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) { // Custom 相当 [[UIButton alloc]init];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//btn 上面有一个imageView
[btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置按钮内容的居左显示
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//设置按钮的内边距
btn.contentEdgeInsets = UIEdgeInsetsMake(, , , );
//设置按钮 label 的 内边距
btn.titleEdgeInsets = UIEdgeInsetsMake(, , , );
//按钮内部 imageview 的内边距
// btn.imageEdgeInsets
//居中显示
btn.imageView.contentMode = UIViewContentModeCenter; //不予许剪切超出部分
btn.imageView.clipsToBounds = NO; [btn addTarget:self action:@selector(nameBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:btn];
self.nameBtn = btn; NSLog(@"---------%@",NSStringFromCGRect(self.contentView.frame)); UILabel *lable = [[UILabel alloc]init]; //居右显示
lable.textAlignment = NSTextAlignmentRight; [self.contentView addSubview:lable]; self.textLbl = lable; }
return self;
} /**
* 代理方法
*/
- (void)nameBtnClick
{
self.group.open = !self.group.open; // if ([self.delegate respondsToSelector:@selector(headerView:)]) { //代理
// [self.delegate headerView:self];
// }
if (self.block) {
self.block(self);
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"friend" object:self userInfo:nil]; //通知 NSLog(@"----------------");
}
/**
* 当 当前的view 加载到父控件的时候调用
*/
- (void)didMoveToSuperview
{ //每次当控件加载到父控件的时候都会调用这个方法,包括init 完一次就会调用一次
if (self.group.open) {
self.nameBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2); NSLog(@""); }else{
self.nameBtn.imageView.transform = CGAffineTransformMakeRotation();
} }
/**
* 当 当前的view 的frame 发生一些改变的时候 调用次方法 重新布局 内部的子控件
*/
- (void)layoutSubviews
{
self.nameBtn.frame = self.bounds;
//获取屏幕的宽度
// CGFloat screenW = [[UIScreen mainScreen] bounds].size.width; CGFloat lblY = ;
CGFloat lblW = ;
CGFloat lblh = self.frame.size.height;
CGFloat lblX = self.frame.size.width - lblW - ; self.textLbl.frame = CGRectMake(lblX, lblY, lblW, lblh); }
@end

IOS第十天(1:QQ好友列表 ,自定义的headview,代理 ,通知 ,black的使用)的更多相关文章

  1. (二十八)QQ好友列表的展开收缩

    要通过监听HeaderView上面的Button来进行操作: 通过addTarget方法即可,应该将按钮的点击方法封装在HearView控制器内部. 列表收起来的原理: tableView: numb ...

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

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

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

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

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

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

  5. ExpandableListView仿QQ好友列表

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

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

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

  7. android 实现QQ好友列表

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

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

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

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

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

随机推荐

  1. 车销送货上门专用无线开单器-自带PDA无线移动开单系统 与云服务器连接

    浩瀚技术配套PDA终端软件 本软件与 数据采集器搭配销售,PDA端软件不单独销售也不含电脑端管理软件 数据采集器 一维扫描头+WIFI+蓝牙+一体打印+PDA软件.  产品特点: 1:通过操作移动手持 ...

  2. poj1222 EXTENDED LIGHTS OUT 高斯消元||枚举

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8481   Accepted: 5479 Description In an ...

  3. zoj 3469 Food Delivery 区间dp + 提前计算费用

    Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving problems, we usual ...

  4. ps让文字的颜色变成图片的颜色

    第一种: 把某张图片图层置于文字图层之上,右击图片图层-->选择"创建剪贴蒙版"即可.第二种:把某张图片图层置于文字图层之上,并选择图片图层,按住Ctrl键,点击文字图层,载 ...

  5. 为WPF和Silverlight的Grid添加边框线(zz)

      Grid是WPF和Silverlight中的一个重要的布局元素,其他的布局元素还有StackPanel, Canvas, Border等等.从字面上说,Grid是一个表格的意思,它的使用也确实很方 ...

  6. 降噪自动编码器(Denoising Autoencoder)

    起源:PCA.特征提取.... 随着一些奇怪的高维数据出现,比如图像.语音,传统的统计学-机器学习方法遇到了前所未有的挑战. 数据维度过高,数据单调,噪声分布广,传统方法的“数值游戏”很难奏效.数据挖 ...

  7. Topcoder SRM 626 DIV2 FixedDiceGameDiv2

    典型的条件概率题目. 事件A在另外一个事件B已经发生条件下的发生概率.条件概率表示为P(A|B),读作“在B条件下A的概率”. 若只有两个事件A,B,那么, P(A|B)=P(AB)/P(B) 本题的 ...

  8. [知识点]C++中的运算符

    1.前言 之前最开始学习语法和基础知识的时候,基本上最简单的运算符有所接触,当时对于位运算这种东西完全没有概念.今天对C++中出现的部分运算符尤其是位运算符进行一些总结. 2.+ - * / % 这些 ...

  9. BZOJ 1798 题解

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 5531  Solved: 1946[Submit ...

  10. Code[VS] 2370 LCA 题解

    Code[VS] 2370 小机房的树 题解 RMQ 树链剖分 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同 ...