简体   繁体   English

在UITableViewCell中检测UIImageView单击

[英]Detect UIImageView click in UITableViewCell

I have to make a call to specific number when imageview is clicked in tableviewcell.The number to which call is made is displayed in a label beside the imageview.But I couldn't get which cell is clicked.Always its referring to the last cell. 当在tableviewcell中单击imageview时,我必须调用特定的数字。调用的数字显示在imageview旁边的标签中。但是我无法获得单击的单元格。总是它指的是最后一个单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    if(cell == nil)
    {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
        lblphone = [[UILabel alloc] initWithFrame:CGRectZero];
        lblphone.tag = 116;
        lblphone.backgroundColor = [UIColor clearColor];
        [lblphone setFont:[UIFont fontWithName:@"Helvetica" size:12]];
        [lblphone setLineBreakMode:UILineBreakModeWordWrap];
        [lblphone setUserInteractionEnabled:YES];

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelButton:)];
        tapGestureRecognizer.cancelsTouchesInView=NO;
        [tapGestureRecognizer setNumberOfTapsRequired:1];
        [lblphone addGestureRecognizer:tapGestureRecognizer];
        [tapGestureRecognizer release];
        [cell addSubview:lblphone];

        myImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        myImageView.tag = 120;
        myImageView.image=[UIImage imageNamed:@"CallImg.png"];

        UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageSelectedInTable:)];
        [tapped setNumberOfTapsRequired:1];
        [myImageView addGestureRecognizer:tapped];
        [tapped release];
        [cell addSubview:myImageView];
    }

    [myImageView setFrame:CGRectMake(10, 50,30,30)];
    myImageView= (UIImageView*)[cell viewWithTag:120];
    myImageView.image=[UIImage imageNamed:@"CallImg.png"];
    myImageView.userInteractionEnabled=YES;

    CGSize constraint5 = CGSizeMake(320, 2000.0f);
    CGSize size5=[phone sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
    lblphone =(UILabel *)[cell viewWithTag:116];
    [lblphone setFrame:CGRectMake(45,name.frame.size.height+name.frame.origin.y,320, size5.height)];
    lblphone.textAlignment=UITextAlignmentLeft;
    lblphone.backgroundColor=[UIColor clearColor];
    lblphone.text=[NSString stringWithFormat:@"%@ ",phone ];
    [lblphone sizeToFit];
}

And in the tapgesture of imageclick I am writing this : 在imageclick的手势中,我正在编写以下代码:

-(IBAction)imageSelectedInTable:(UITapGestureRecognizer*)gesture
{
    UIImageView *imgView = (UIImageView *) [gesture view];
    UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForCell:cell];
    NSLog(@"Image Tap %@", tappedIndexPath);
    UILabel *phoneno = (UILabel *)[cell viewWithTag:116];

    NSString *phoneNumber = [@"telprompt://" stringByAppendingString:phoneno.text];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}

But it is always referncing to the last cell clicked irrespective of which cell is clicked.Couldn't understand where im going wrong ? 但它总是指向最后一个单击的单元格而不管单击哪个单元格。不明白我哪里出错了?

It looks like the image view is a subview of the cell (one step down the tree), and the code that's probing for the parent is taking two steps up ...superview superview. 看起来图像视图是单元格的子视图(在树下一步),并且正在探测父级的代码需要两步...... superview superview。 (That latter approach is right for nib defined cells which add subviews to the cell's contentView). (后一种方法适用于笔尖定义的单元格,该单元格将子视图添加到该单元格的contentView中)。

Here's a safer way to climb the view hierarchy that will work no matter how the cell was built. 这是一种爬取视图层次结构的更安全的方法,无论单元如何构建,该视图层次结构都将起作用。

- (UITableViewCell *)tableViewCellContaining:(UIView *)aView {

    UIView *answer = aView;
    while (answer && ![answer isKindOfClass:[UITableViewCell self]])
        answer = answer.superview;
    return answer;
}

I think your code will probably work if you replace this line: 我认为如果替换以下行,您的代码可能会起作用:

UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];

with this line 用这条线

UITableViewCell *cell = [self tableViewCellContaining:imgView]

I see you try to access your cell, 我看到您尝试访问您的手机,

UITableViewCell *cell = (UITableViewCell*)[[imgView superview]superview];

but you add your image view to 但是您将图像视图添加到

[cell addSubview:myImageView];

You should be adding your image view to cell.contentView . 您应该将图像视图添加到cell.contentView

for get/know Which cell is clicked, Write Following code 获取/知道单击了哪个单元格,编写以下代码

UITableViewCell *cell = (UITableViewCell *)[[imgView superview]superview];

and if you alSo want to get indexPath of tapped cell then write 如果你想要获取tapped cell的indexPath然后写

NSIndexPath *indexPath = [tableView indexPathForCell:cell];

EDITED :-> Another way is 编辑: - >另一种方式是

Give tag of UIImageView in cellForRowAtIndexPath Method Such like, cellForRowAtIndexPath方法中给UIImageView tag ,例如

imageView.tag = indexPath.row; 

And Use Following Code 并使用以下代码

int row = ((UIImageView*)imgView).tag; //Or// int row = ((UIGestureRecognizer *)sender).view.tag;

NSIndexPath* indexpath = [NSIndexPath indexPathForRow:row inSection:0]; 
UITableViewCell* cell = [table cellForRowAtIndexPath:indexPath];

Here You can get Cell of Tapped UIImageView . 在这里你可以得到Cell螺纹的UIImageView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM