博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在cell内部操作控制器的一些行为
阅读量:7091 次
发布时间:2019-06-28

本文共 1224 字,大约阅读时间需要 4 分钟。

场景:

比如cell内部存在button, 或者cell内部嵌套一个collectionView, 此时在点击cell上的button或者collectionView的item时,希望控制器进行跳转之类的行为

(当然使用代理模式也能实现,在这里记录一下另一种方式)

 

思路:将target和SEL(action)传递到cell内部, 用 - (id)performSelector:(SEL)aSelector withObject:(id)object;方法, 达到在cell内调用方法,但在控制器中实现

 

1、在cell的.h文件中

- (void)setTarget:(id)target action:(SEL)action;

 

2.在cell的.m文件中添加两个属性

@property (weak, nonatomic) id target;

@property (assign, nonatomic) SEL action;

 

//在.m中实现.h的方法

- (void)setTarget:(id)target action:(SEL)action

{

  self.target = target;

  self.action = action;

}

//在cell中button的触发方法中或在cell的collectionView的didSelectItemAtIndexPath方法中调用方法

- (void)xxxxxxxxxxxxxx

{

  if(self.target && self.action)

  {

  [self.target performSelector:self.action withObject:indexPath];//这里可以带参数

  }

}

 

3. 这时便可在控制器中创建cell的时候 将cell调用自己的方法

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

  xxxcell *cell = [tableView dequeueReusableCellWithIdentifier:  forIndexPath:];

  [cell setTarget:self action:@selector:(push:)];

}

- (void)push:(xxxcell*)sender  //sender即为cell中传的参数

{

  [self.navigationController pushViewController:xxVC animated:YES];

}

 

就这样达到了想要的效果, 在cell内部操作控制器的一些行为!

 

转载于:https://www.cnblogs.com/Tempation/p/6084772.html

你可能感兴趣的文章
nvidia显卡驱动卸载和卸载后的问题
查看>>
Java集合源码分析(二)Linkedlist
查看>>
SpringBoot四大神器之Actuator
查看>>
html复习之标签整理
查看>>
Yii2 使用 faker 生成假数据(转)
查看>>
Consul安装使用
查看>>
tomcat事件处理机制
查看>>
JS BUG 传递数字过大,数据值会变化
查看>>
橡皮筋进度条ElasticProgressBar
查看>>
spring boot引入json,jsonobject,需要指定jdk15
查看>>
企业架构 - 涉众管理(Stakeholder Management)
查看>>
Ubuntu11.10 解决rar文件解压错误
查看>>
sqlplus: error while loading shared libraries: /u01/app/lib/libclntsh.so.11.1
查看>>
ORACLE等待事件:enq: TX - row lock contention
查看>>
使用Fiddler2录制HTTP操作脚本
查看>>
响应activex事件
查看>>
Winform 进程之间通讯的几种方法
查看>>
c++中冒号(:)和双冒号(::)的用法
查看>>
dubbo工作原理
查看>>
驱动开发利器Microsoft Windows Driver Kit 7.1.0下载
查看>>