自定义Cell上的点击事件两种方法
2015-11-03 15:27
381 查看
方法一是代理:这里先忽略,不写!
方法二是Block:
1、在Cell的类中需要定义一个block块的类型变量,用于在ViewController中使用;
2、在Cell中声明CellBlock类型的变量,用于接收回调;
3.添加设置block的setter方法,参数是要传入的block块
方法二是Block:
1、在Cell的类中需要定义一个block块的类型变量,用于在ViewController中使用;
//创建cell的block块把按钮的tag传到ViewController中 typedef void (^CellBlock) (CCustomTableViewCell * cell, UIButton *button);
2、在Cell中声明CellBlock类型的变量,用于接收回调;
@property (strong, nonatomic) CellBlock block;
3.添加设置block的setter方法,参数是要传入的block块
-(void)setTagButtonBlock:(CellBlock)cellBlock { self.block = cellBlock; }
<span style="color: rgb(51, 51, 51); font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; line-height: 26px;">4、点击不同的button是给传入的block设置不同的值,也就是把Button的tag传入到block中。添加的三个按钮对应着一个回调方法,代码如下:</span>
- (void)btnClick:(UIButton *)btn{ self.block(self, btn); }5、在我们的TableView中实现Cell的回调,根据回调参数Button.tag的值的不同,去执行相应的业务逻辑,回调的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.创建cell CCustomTableViewCell *cell = [CCustomTableViewCell cellWithTableView:tableView]; [cell setTagButtonBlock:^(CCustomTableViewCell *cell, UIButton *btn) { [self agreeOrComment:btn andIndex:indexPath]; }]; // 2.在这个方法算好了cell的高度 cell.statusFrame = self.statusFrames[indexPath.row]; //3.返回一个cell return cell; }
相关文章推荐
- 信息提示框Toast
- GATT层
- Reflow(渲染)和Repaint(重绘)
- left join
- java基础复习02
- java基础复习02
- LU分解
- iBatis的SQL注入问题
- Android开发之输入法详解
- maven项目如何生成war文件
- Maven环境配置
- cmd下windows批处理,获取当前系统时间,生成日志文件名
- UINavigationController层次关系
- C++数据结构环形队列Deque实现
- Aspose系列(二)Word增加页眉和页脚
- (五):Winelib开发组件(1)
- 浏览器控制台验证正则
- 【基础练习】【并查集】codevs2796 最小完全图题解
- reviewboard升级到2.5版本后summary列表为none
- PHP display_errors与error_reporting