2011-09-09 19 views
3

私のUITableViewには1つのセルしかなく、セルにUIButtonを追加します。 touchUpInsideイベントが発生したときにアクションを設定します。しかし、それは全く機能しません。一方、 がtouchUpInsideの代わりにtouchDownイベントとしてトリガーを設定すると、設定されたアクションが発生します。UIButtonのイベントtouchUpInsideが機能しない

グーグルで理由を調べると、問題はおそらくスーパービューのディメンションによって発生していると言われます。つまり、ボタンのサイズはスーパービューを超えています。 は、私はボタンを含むようにする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

最近この問題が発生しました。あなたのUITableViewUITapGestureというオブジェクトが追加されている必要があります。その場合は、UITapGestureオブジェクトのcancelsTouchesInViewプロパティをNOに設定してください。例えば

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:tapAction]; 
gesture.cancelsTouchesInView = NO; 
[self addGestureRecognizer:gesture]; 
関連する問題