今天结合一个实际的demo 来给大家讲解一下代理方法的运用
首先介绍一下 什么是代理,有什么用
/** 可以提高代买的复用性
* 代理的作用1.封装变化点
2.解决类与类之间的强耦合性
3.可以在类之间进行数据传递
什么情况下使用代理好呢
1.有一系列变化点(比如:UIScrollView 有许多方法,)
能够让用户清晰的知道到底有哪些变化点被封装起来了
*/
我们在项目开发的过程中会用到很多的代理,block
两个之间有区别也有联系,代理和block 都可以用来传值
代理实现起来步骤比较多
简单的来说,代理就是委托,比如老板需要做一些事儿,老板不想去做,这时委托员工去做
那么员工就是老板的代理,员工是被动一方,老板是主动的一方
我们一般会拿协议来规范代理的行为,代理需要遵守协议,当发生委托时,老板就会通知代理去完成
代理可以实现对象之间的相互通信
今天结合UICollectionView 来讲解一下代理的运用
这个demo是按照mvc 的思想来完成的
我们想生产一个组件来提供给别人使用
我们需要把自己的组件安装在控制器之中,那么我们需要一个容器来存放这个UICollectionView
那么我们就提供一个UIVIew 来存放
新建一个类 继承自UIView 我们将UICollectionView 放在上面
这里还提供了一个自定义Item(cell) 使用xib 建立的,我们需要将cell 安装在UICollectionView上
自定义Item 有两个小空间 - 一个button 一个Label
我们想实现 :当用户点击cell 的button 时 我们把Label 的内容传递给我们
有两种解决方案:一种是利用用户交互,给cell 绑定事件,我们可以获得cell 的对象
另一种:设置代理
方法如下:
#import <UIKit/UIKit.h>
@interface QHCollectionView : UIView
+(instancetype)collectionView;
@end
#import "QHCollectionView.h"
#import "QHCollectionCell.h"
@interface QHCollectionView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,QHCollectionCellDelegate>
@property(nonatomic,strong)UICollectionView *collectionView;
@end
@implementation QHCollectionView
+(instancetype)collectionView
{
return [[self alloc]init];
}
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = collectionView;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.pagingEnabled = YES;
self.collectionView.showsHorizontalScrollIndicator = NO;
// self.collectionView
}
return self;
}
#pragma mark UICollectionViewDataSource 协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//注册机制
/*
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ABC"];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ABC" forIndexPath:indexPath];
*/
QHCollectionCell *cell = [QHCollectionCell cellWithCollectionView:collectionView andIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
cell.delegate = self;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"8324823987");
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(200, 200);
}
-(void)collectionCellWithBtnClick:(QHCollectionCell *)qhCollection andTextInfo:(NSString *)textInfo
{
NSLog(@"百度员工号:%@", textInfo);
NSLog(@"%@",NSStringFromSelector(_cmd));
}
-(void)willMoveToSuperview:(UIView *)newSuperview
{
self.frame = newSuperview.bounds;
self.backgroundColor = [UIColor purpleColor];
self.collectionView.frame = self.frame;
self.collectionView.backgroundColor = [UIColor yellowColor];
//建立父子关系 设置frame
[self addSubview:self.collectionView];
//根据父控件设置子控件的frame
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
#import <UIKit/UIKit.h>
@class QHCollectionCell;
@protocol QHCollectionCellClickDelegate <NSObject>
-(void)collectionCollectionClellClick:(QHCollectionCell *)qhCollection andCellView:(UIView *)view;
@end
@protocol QHCollectionCellDelegate<NSObject>
-(void)collectionCellWithBtnClick:(QHCollectionCell *)qhCollection andTextInfo:(NSString *)textInfo;
@end
@interface QHCollectionCell : UICollectionViewCell
@property(nonatomic,assign)id <QHCollectionCellDelegate>delegate;
@property(nonatomic,assign)id <QHCollectionCellClickDelegate>delegateC;
+(id)cell;
+(id)cellWithCollectionView:(UICollectionView *)collection andIndexPath:(NSIndexPath *)indexPath;
@end
#import "QHCollectionCell.h"
@interface QHCollectionCell()
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@end
@implementation QHCollectionCell
+(id)cell
{
return [[self alloc]init];
}
+(id)cellWithCollectionView:(UICollectionView *)collection andIndexPath:(NSIndexPath *)indexPath
{
NSString *cellName = NSStringFromClass([self class]);
UINib *nib = [UINib nibWithNibName:cellName bundle:nil];
[collection registerNib:nib forCellWithReuseIdentifier:cellName];
QHCollectionCell * cell = [collection dequeueReusableCellWithReuseIdentifier:cellName forIndexPath:indexPath];
// UITapGestureRecognizer * tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tgrClick:)];
// cell.userInteractionEnabled = YES;
// [cell addGestureRecognizer:tgr];
return cell;
}
-(void)tgrClick:(UITapGestureRecognizer *)tgr
{
NSLog(@"%@",tgr.view);
[_delegateC collectionCollectionClellClick:self andCellView:tgr.view];
}
- (IBAction)btnClick:(id)sender {
NSString *textLabelInfo = self.textLabel.text;
//NSLog(@"%@",self.textLabel.text);
[self.delegate collectionCellWithBtnClick:self andTextInfo:textLabelInfo];
NSLog(@"%@",self.delegate);
NSLog(@"点击");
}
@end