0

我正在制作一个 ipad 应用程序,其中我有自定义的 tableView 它工作正常,但我希望当用户单击自定义单元格中的添加按钮时,它应该获取该按钮的位置 x 和 y,然后将 imageView 设置为那个按钮。

我已经搜索了它得到积分的代码,但是如何为 cell.addButton 添加它,因为单元格是自定义的。

        [cell.imageButton addTarget:self action:@selector(imageButtonActionOne:) forControlEvents:UIControlEventTouchUpInside];
    [cell.addButton addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];

这是触摸的代码

    UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
   [cell.addButton addGestureRecognizer:rec];
    [rec release];

但是这样它不会调用cell.addButton imageButtonAction 上的方法。

4

2 回答 2

1

据我了解,您需要在点击时在 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 添加按钮,但它没有被点击

于 2013-07-04T09:57:27.303 回答
0

对于 UIButton 您可以直接使用按钮功能

-(IBAction) imageButtonAction:(id) sender
{
  UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
  UIButton *btn = (UIButton *)sender;
  //[btn setimage:[UIImage imagenamed:dimagename];
  UIImageview *backimage = [[UIImageView alloc] initWithFrame:btn.frame];
  [cell addSubview:backimage];
}       
于 2013-07-04T09:57:08.603 回答