为什么我自定义单元格中的单击单击事件按钮无法接收

时间:2017-08-03 01:10:49

标签: ios objective-c uibutton

我已经定制了这样的视图:

 #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没有被调用,伙计们,我该怎么办?

1 个答案:

答案 0 :(得分:0)

在您的单元格实现中,您实现了click方法,但没有目标来调用该方法。因此您可以尝试使用protocol delegate或block。