在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数

在UITableView或UICollectionView的自定义的cell里添加手势或button时,有时会可能会需要用到选中的cell需要传出cell的位置,下面就两种情况给出解释

一:button响应方法如何传参

方法1,用代理

@protocol CustomCellProtocol
  - (void)customCell:(CustomCell *)cell didTapButton:(UIButton *)button;
@end
      将UIButton的响应加在CustomCell文件中,比如这个响应叫做
      那么在点击button时,就会调用这个方法。这个方法可以实现如下,
{
  if ([self.delegate respondsToSelector:@selector(customCell:didTapButton:)])
  {
    [self.delegate performSelector:@selector(customCell:didTapButton:) withObject:self   withObject:self.button];
  }
}
      然后在UIViewController里实现代理方法,这样,就能获得这个UIButton所在的UITableViewCell。- (IBAction)buttonTarget:(id)sender;

方法2,自定义一个button,给button个属性,

            [button addTarget:selfaction:@selector(btnMethod:)forControlEvents:UIControlEventTouchUpInside];

            [button setTag:j];

            [button setTestStr:@"我是测试信息么么哒"];


///button的点击方法

- (void)btnMethod:(YBCustomButton *)sender

{

    /**

     *  button响应点击方法传入参数,传入的是tag

     */


    NSInteger i = [sender tag];

    NSString *string = sender.testStr;

    NSLog(@"..................%ld",(long)i+1);

    NSLog(@"...%@",string);

    

}





二、手势响应方法时如何传参

步骤:

1、自定义手势

       @interface YBCustomGesture : UITapGestureRecognizer

       @property (nonatomic)NSInteger tag;

@end

2、调用属性的set方法

        YBCustomGesture *labelGesture = [[YBCustomGesture alloc]initWithTarget:self action:@selector(labelTest:)];

        [labelGesture setTag:i];

3、

//label的添加的点击手势方法

- (void)labelTest:(YBCustomGesture *)sender

{

    /**

     *  用自定义的UIGestureRecognizer来传递参数,达到传参目的。

     */

    NSInteger integer = sender.tag;

    NSLog(@"+++++++%ld",(long)integer);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值