IOS自定义View实现相应的控件点击方法以及代理的总结(附代码)
发布日期:2021-11-21 04:40:56
浏览次数:38
分类:技术文章
本文共 2872 字,大约阅读时间需要 9 分钟。
继续完善
我们想在点击cell的时候能够做出相应的反应,现在着手做吧
1.我们在IKEDMyOwnHorizenView.m文件下添加下面代码
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"%@",[[collectionImageData objectAtIndex:[indexPath row]] objectForKey:@"title"]);}结果是能够打印出标题的,如果我们想让这个标题在界面上打印出来,该怎么办呢?这就是传递参数的问题了,最好的回传参数的问题就目前我学习的范围来说是代理传值(PS:本人新手,所以针对我来说的,学无止尽,我需要更加努力了)
2.创建代理(最近看了一本教程,虽然有点老但是收获蛮大)
(1)在头文件中声称自己要引用的类,其实也就是自己
(2)在.h文件定义代理对象要实现的协议
(3)将代理对象最为自己的一个属性存在
(4)在被代理对象合适的时候执行代理方法
(5)在代理对象的头文件或者.m文件中声明代理
(6)实现代理对象的方法
(7)告诉被代理对象代理对象是谁
好了,总结完毕,上代码了
3.在IKEDMyOwnHorizenViewDelegate.h文件实现代理声明以及相应方法的声明
//// IKEDMyOwnHorizenView.h// Ikefr//// Created by apple on 14-2-27.// Copyright (c) 2014年 com.tyust. All rights reserved.//#import4.在用户点击cell的时候调用代理方法,在IKEDMyOwnHorizenView.文件中添加下面的代码@class IKEDMyOwnHorizenView;@protocol IKEDMyOwnHorizenViewDelegate -(void)getData:(IKEDMyOwnHorizenView *)ikedView fromSelectedCellAtIndexpathString:(NSString*)str;@end@interface IKEDMyOwnHorizenView : UIView @property(weak,nonatomic)id delegate;@property(nonatomic,strong)UICollectionView *myCollectionView;-(void)setImgData:(NSArray*)array;@end
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSString *str =[[collectionImageData objectAtIndex:[indexPath row]] objectForKey:@"title"]; [self.delegate getData:self fromSelectedCellAtIndexpathString:str]; NSLog(@"sadsad");}5.在代理对象中实现相应的代理方法,在IKEDMyOwnHorizenView.m中实现代理方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSString *str =[[collectionImageData objectAtIndex:[indexPath row]] objectForKey:@"title"]; [self.delegate getData:self fromSelectedCellAtIndexpathString:str]; }6.在实例化被代理对象的时候,就告诉他的代理对象是谁
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; flowLayout.itemSize = CGSizeMake(100, 100); flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10); flowLayout.minimumInteritemSpacing = 10; _myCollectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout]; _myCollectionView.dataSource = self; _myCollectionView.delegate = self; _myCollectionView.showsHorizontalScrollIndicator = NO; self.backgroundColor = [UIColor whiteColor]; [_myCollectionView registerClass:[IKEDMyOwnImgView class] forCellWithReuseIdentifier:@"IKeD"]; [self addSubview:_myCollectionView]; } return self;}
7.大功告成,结果如下
转载地址:https://blog.csdn.net/xiaoji94shen/article/details/20049223 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月10日 21时41分38秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Leetcode刷题篇】leetcode169 多数元素
2019-04-26
【Leetcode刷题篇】leetcode461 汉明距离
2019-04-26
【Leetcode刷题篇】leetcode204 计数质数
2019-04-26
【Leetcode刷题篇】leetcode70 爬楼梯
2019-04-26
【Leetcode刷题篇】leetcode739 每日温度
2019-04-26
【Leetcode刷题篇】leetcode121买卖股票的最佳时机
2019-04-26
【面试篇】Java多线程并发-Java关键字volatile详解
2019-04-26
【面试篇】Java的代理模式-静态代理和动态代理详解
2019-04-26
【面试篇】 Java对象拷贝(对象克隆 对象复制)
2019-04-26
【Leetcode刷题篇】leetcode64 最小路径和
2019-04-26
【Leetcode刷题篇】leetcode79 单词搜索
2019-04-26
【Leetcode刷题篇】leetcode300 最长上升子序列
2019-04-26
【Leetcode刷题篇】leetcode394 字符串解码
2019-04-26
【Leetcode刷题篇】leetcode152 乘积最大数组
2019-04-26
【Leetcode刷题篇】leetcode56 合并区间
2019-04-26
【Leetcode刷题篇】leetcode210 课程表II
2019-04-26
【Leetcode刷题篇】leetcode207 课程表
2019-04-26
【Leetcode刷题篇】leetcode322 零钱兑换
2019-04-26
【Leetcode刷题篇】leetcode437 路径总和III
2019-04-26
【Linux篇】Linux常用命令之性能优化
2019-04-26