2011-09-09 99 views
3

在我的UITableView中只有1個單元格,並且我向該單元格中添加了一個UIButton。 設置touchUpInside事件發生時的動作。但它根本不起作用。同時,如果 我將trigger設置爲touchDown事件而不是touchUpInside,它會觸發我設置的操作。事件touchUpInside的UIButton不起作用

我搜索谷歌的原因,有人說,這個問題可能是由superview的維度造成的。那就是按鈕的大小超出了它的超級視角。 所以我添加一個UIView以包含按鈕,代碼如下:

self.ageDisplay = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 
UIImage *ageImage = [UIImage imageNamed:@"long_btn_click.png"]; 
[self.ageDisplay setImage:ageImage forState:UIControlStateSelected]; 
[self.ageDisplay setFrame:CGRectMake(10, 5, 145, 34)];//(10, 320, 145, 25)]; 
[self.ageDisplay setTitle:@"請選擇您的年齡" forState:UIControlStateNormal]; 
[self.ageDisplay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[self.ageDisplay setTag:3]; 
[self.ageDisplay addTarget:self action:@selector(showSelectionPage:) forControlEvents:UIControlEventTouchDown]; 

self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 310, 320, 106)]; 
//self.buttonsSubView.backgroundColor = [UIColor blueColor]; 
[self.buttonsSubView setUserInteractionEnabled:YES]; 
[self.buttonsSubView addSubview:self.ageDisplay]; 

上面的代碼是在viewDidLoad中的功能。

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *sugPageCell = @"SuggestionPageCell"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:sugPageCell]; 
    if (nil == cell) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sugPageCell]; 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 


    [cell addSubview:self.sugTitle]; 
    [cell addSubview:self.sugSpecification]; 
    [cell addSubview:self.suggestion]; 
    self.buttonsSubView.userInteractionEnabled = YES; 
    [cell addSubview:self.buttonsSubView]; 

    /* 
    [cell addSubview:self.ageDisplay]; 
    [cell addSubview:self.genderDisplay]; 
    [cell addSubview:self.submit]; 
    */ 

    return cell; 

} 

但是,這並沒有幫助。 這個問題困擾了我2天。 如果有人能提供一些指示,我將不勝感激。 謝謝。

回答

4

這是因爲你的buttonSubView視圖在單元格的框架之外。 所以即使按鈕是可見的(因爲單元的clipsToBounds設置爲NO),它將不會從單元接收觸摸事件。爲什麼你將這個視圖的垂直位置設置爲310?

如果你試試這個:

self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 106)]; 

事件應激發你想要的。

+0

感謝您的解決方案... –

1

我最近有這個問題。我猜想可能有一個UITapGesture的對象添加到您的UITableView。如果是這樣,請將UITapGesture對象的屬性cancelsTouchesInView設置爲否。

例如:

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:tapAction]; 
gesture.cancelsTouchesInView = NO; 
[self addGestureRecognizer:gesture];