IOS第十天(1:QQ好友列表 ,自定义的headview,代理 ,通知 ,black的使用)
*****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的使用)的更多相关文章
- (二十八)QQ好友列表的展开收缩
要通过监听HeaderView上面的Button来进行操作: 通过addTarget方法即可,应该将按钮的点击方法封装在HearView控制器内部. 列表收起来的原理: tableView: numb ...
- 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 ...
- 仿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 ...
- 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 ...
随机推荐
- const int *
5.Please choose the right statement about constusage: A.const int a;//const interger B.int const a;/ ...
- 疯狂java学习笔记之面向对象(三) - 方法所属性和值传递
方法的所属性: 从语法的角度来看:方法必须定义在类中 方法要么属于类本身(static修饰),要么属于实例 -- 到底是属于类还是属于对象? 有无static修饰 调用方法时:必须有主调对象(主语,调 ...
- 每天一个linux命令---导出到文件
导出Linux下的部分日志到文件,使用‘>’符号 例如: [calendar@test190 logs]$ monitor.log|grep getCalendarView > share ...
- Shell 编程基础之变量和环境变量
一.变量赋值和引用 Shell 编程中,使用变量无需事先声明,同时变量的命名不惜遵循如下规则: 首个字符必须为字母(a-z,A-Z)或者_ 变量名中间不能有空格,可以使用_连接 不能使用其他表达符号 ...
- 来抢你们IT狗的饭碗了
Java,Html,Css,JavaScript,Jsp啥都不会啊 ,等着我!
- Windows和Linux(Ubuntu)下安装Scala及ScalaIDE
1.下载 1.1Scala下载 Windows版:http://www.scala-lang.org/download/ Linux版:http://www.scala-lang.org/downlo ...
- Linux下使用vim的tips
1.如果用户不能确定vi所处的状态,可以按Esc键两次返回初始状态. 2.Fedora 17下安装与配置ssh:http://blog.csdn.net/ashuai81/article/detail ...
- pig相关
1. 重命名pig job name: 在Pig脚本中的一开始处,写上这一句: set job.name 'This is my job'; 2. 设置pig参数: 允许pig时,输入如下: pig ...
- 自适应学习率调整:AdaDelta
Reference:ADADELTA: An Adaptive Learning Rate Method 超参数 超参数(Hyper-Parameter)是困扰神经网络训练的问题之一,因为这些参数不可 ...
- 移动端页头推荐配置 出现找不到favicon.ico错误原因和解决方法
favicon 在未指定 favicon 时,大多数浏览器会请求 Web Server 根目录下的 favicon.ico .为了保证 favicon 可访问,避免404,必须遵循以下两种方法之一: ...