2017-08-03 20 views
1

我已自定義視圖這樣的:爲什麼我定製在細胞中的細胞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在細胞不叫,夥計們,我該怎麼辦?

回答

0

在您的單元實現中,實現了click方法,但沒有調用method.so的目標,所以您可以嘗試使用協議委託或塊。

+0

是的,你說得對,我認爲目標是零,系統會按照響應鏈找到下一級的下一級還沒有達到SEL,所以我在SEL的單元中實現了。但很奇怪,並沒有打電話到這裏。順便說一句,如果自定義視圖和控制器和委託實現這個SEL,系統會調用,我測試過 –