开发者

the event touchUpInside of a UIButton don't work

there is only 1 cell in my UITableView, and I add a UIButton to the cell. set the action when touchUpInside event happens. But it doesn't work at all. Meanwhile, if i set the trigger as touchDown event instead of touchUpInside, it will fire the action i set.

I search the reason by google, someone says that the problem maybe caused by the superview's dimension. That is the size of the button is beyond its superview. so I add a UIView to contain the button, the code is as follow:

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];

the above code is in ViewDidLoad function.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *sugPageCell = @"SuggestionPageCell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:sugPageCell];
    if (nil == cell)
    {
        cell = [[UITableViewCel开发者_StackOverflowl 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;

}

However, this doesn't help. The problem has confused me for 2 days. I will appreciate if anyone can provide some indication. Thank you.


It's because your buttonSubView view is outside the cell's frame. So even if the button is visible (because the cell's clipsToBounds is set to NO), it will not receive touch events from the cell. Why do you set this view's vertical position to 310 ?

If you try this :

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

The event should fire as you want.


I had this problem recently. I guess there must may be an object of UITapGesture added to your UITableView. If so, please set the property cancelsTouchesInView of the UITapGesture object to NO.

For example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜