据我了解,您需要在点击时在 UIButton 中设置图像。
如果您添加了 UITapGestureRecognizer 您删除了默认的点击识别,现在只有这样才能工作:
UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
[cell.addButton addGestureRecognizer:rec];
[rec release];
你应该删除上面的代码,只设置这个:
[cell.addButton addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
其中imageButtonAction:是:
- (void )imageButtonAction:(UIButton *) b
{
[b setImage:[UIImage imageNamed:@"img.png"] forState:UIControlStateNormal];
}
如果您想在按钮旁边添加图像,比如说在按钮的左侧,那么函数应该如下所示:
- (void )imageButtonAction:(UIButton *) b
{
UITableViewCell *cell = (UITableViewCell*)[b superview];
NSInteger imgWidth = 50;
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(b.frame.origin.x - imgWidth,b.frame.origin.y,imgWidth,b.frame.size.height)];
img.backgroundColor = [UIColor redColor];
[cell addSubview:img];
[img release];
}
UIButton *btn = (UIButton *)sender;
UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(10,0,312,105)];
//If you want to do this set interaction enabled.
backimage.userInteractionEnabled = YES;
backimage.image=[UIImage imageNamed:@"popupj.png"];
tab1 = [UIButton buttonWithType:UIButtonTypeCustom];
[tab1 setFrame:CGRectMake(-1,2,293,58)];
[tab1 setTitle:@"Record Video" forState:UIControlStateNormal];
[tab1 addTarget:self action:@selector(tab1Action) forControlEvents:UIControlEventTouchUpInside];
[tab1 setImage:[UIImage imageNamed:@"popuptab1j.png"] forState:UIControlStateNormal];
[backimage addSubview:tab1];
现在我正在向 imageView 添加按钮,但它没有被点击