您的位置:首页 > 其它

自定义Cell上的点击事件两种方法

2015-11-03 15:27 381 查看
方法一是代理:这里先忽略,不写!

方法二是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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: