为什么我定制在细胞中的细胞click事件的按钮无法接收

问题描述:

我已自定义视图这样的:为什么我定制在细胞中的细胞click事件的按钮无法接收

#import "LWView.h" 
    @implementation LWView 
    - (instancetype)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:CGRectMake(frame.origin.x, frame.origin.y , frame.size.width, frame.size.height)]) { 
     self.backgroundColor = [UIColor lightGrayColor]; 
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
     btn.frame = CGRectMake(0, 0, 40, 40); 
     [btn setTitle:@"点我" forState:UIControlStateNormal]; 
     [btn addTarget:nil action:@selector(click) forControlEvents:UIControlEventTouchUpInside]; 
     btn.backgroundColor = [UIColor redColor]; 
     [self addSubview:btn]; 
    } 
    return self; 
    } 
    @end 

是的,我设定的目标为nil因为我想这个观点加入到我的自定义单元格,我想我的手机来实现这个SEL,这是自定义单元代码:

#import "LWTableViewCell.h" 
    #import "LWView.h" 
    @implementation LWTableViewCell 
    - (instancetype)init { 
    if (self = [super init]) { 
     LWView *lwView = [[LWView alloc]initWithFrame:CGRectMake(0, 0, 44, 44)]; 
     [self.contentView addSubview:lwView]; 
    } 
    return self; 
} 

     - (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 
} 

    - (void)click { 
    NSLog(@"U click me!"); 
    } 
    @end 

我实现了在tableview中使用自定义单元格的,但是当我点击这个按钮时,SEL在细胞不叫,伙计们,我该怎么办?

在您的单元实现中,实现了click方法,但没有调用method.so的目标,所以您可以尝试使用协议委托或块。

+0

是的,你说得对,我认为目标是零,系统会按照响应链找到下一级的下一级还没有达到SEL,所以我在SEL的单元中实现了。但很奇怪,并没有打电话到这里。顺便说一句,如果自定义视图和控制器和委托实现这个SEL,系统会调用,我测试过 –