*****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. 工具:使用jekyll生成静态网站

    在使用前端框架构建网页而不使用后端平台与数据库,即没有服务器的条件下读取文件夹其他文件,浏览器可能会阻止访问.对于这种静态构建可以使用简单的生成工具jekyll.它主要适用于将静态文件生成静态网站,在 ...

  2. jQuery跨域

    其实jQuery跨域很简单很简单,你记住格式就好,跨域的原理请参考 <jsonp跨域> jQuery跨域代码: $.ajax({ url:'https://suggest.taobao.c ...

  3. HDU5816 Hearthstone(状压DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collec ...

  4. memcache/redis 缓存学习笔记

    0.redis和memcache的区别 a.redis可以存储除了string之外的对象,如list,hash等 b.服务器宕机以后,redis会把内存的数据持久化到磁盘上,而memcache则不会 ...

  5. phpstorm 10 修改背景图片和字体

    修改menu:File ~ Settings ~ Appearance & Behavior ~ Appearance ~ Theme 改成 Darcula即成黑色背景 menu字体大小: 编 ...

  6. Storm配置项详解【转】

    Storm配置项详解 ——阿里数据平台技术博客:storm配置项详解 什么是Storm? Storm是twitter开源的一套实时数据处理框架,基于该框架你可以通过简单的编程来实现对数据流的实时处理变 ...

  7. POJ 2096 (概率DP)

    题目链接: http://poj.org/problem?id=2096 题目大意:n种bug,s个子系统.每天随机找一个bug,种类随机,来自系统随机.问找齐n种bug,且每个子系统至少有一个bug ...

  8. POJ 1753 (开关问题+高斯消元法)

    题目链接: http://poj.org/problem?id=1753 题目大意:一堆格子,或白或白.每次可以把一个改变一个格子颜色,其上下左右四个格子颜色也改变.问最后使格子全部白或全部黑,求最小 ...

  9. 在Web应用中接入微信支付的流程之极简清晰版 (转)

    在Web应用中接入微信支付的流程之极简清晰版 背景: 在Web应用中接入微信支付,我以为只是调用几个API稍作调试即可. 没想到微信的API和官方文档里隐坑无数,致我抱着怀疑人生的心情悲愤踩遍了丫们布 ...

  10. 20145304 Java第八周学习报告

    20145304<Java程序设计>第八周学习总结 教材学习内容总结 NIO NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量,在缓冲区中对感兴趣的数据区块进行标记 ...