UITutView在UITableViewCell中没有响应

时间:2011-03-28 19:22:03

标签: iphone objective-c xcode ipad uitableview

if ([cell.textLabel.text isEqualToString:@"Button"])
{
    UIView *ButtonView = [[UIView alloc] initWithFrame:CGRectMake(100, 5, 100, 25)];
    UIButton *Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    Button.frame = CGRectMake(90, 5, 90, 30);
    [Button setTitle:@"PopOver" forState:UIControlStateNormal];
    [ButtonView addSubview:Button];
    [cell.contentView addSubview:ButtonView];
    [Button addTarget:self action:@selector(showPickerPopupAction)forControlEvents:UIControlEventTouchUpInside]; 
    Button.tag = indexPath.row; 
}

- (void)showPickerPopupAction {     NSLog(@“按钮点击。”); }

2 个答案:

答案 0 :(得分:0)

Button的框架几乎完全位于ButtonView的框架之外。如果在ButtonView上设置backgroundColor,您将看到只有按钮的左上角位于ButtonView框架中,只有该角落会响应触摸。

调整ButtonView框架的大小或Button框架的大小和位置,使按钮完全位于ButtonView内。

不确定为什么首先需要ButtonView。您可以直接将Button添加到contentView。

此外,在将ButtonView添加到cell.contentView后,您应该[ButtonView release]

答案 1 :(得分:0)

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:@"ButtonCell"] autorelease];

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
backgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];

cell.backgroundView = backgroundView;

UIButton *buttonLeft = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 [buttonLeft setTitle:@"Left" forState:UIControlStateNormal];
 [buttonLeft setFrame: CGRectMake( 10.0f, 3.0f, 145.0f, 40.0f)];
 [buttonLeft addTarget:self action:@selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];

 UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonRight setTitle:@"Right" forState:UIControlStateNormal];
[buttonRight setFrame: CGRectMake( 165.0f, 3.0f, 145.0f, 40.0f)];
[buttonRight addTarget:self action:@selector(addToFavorites) forControlEvents:UIControlEventTouchUpInside];

[cell addSubview:buttonLeft];
[cell addSubview:buttonRight];

[backgroundView release];

-(void) addToFavorites {}

Works fine !!