2

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

在 imageclick 的 Tapgesture 中,我正在写这个:

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

但它总是引用最后一个单击的单元格,而不管单击哪个单元格。不明白我哪里出错了?

4

3 回答 3

3

看起来图像视图是单元格的子视图(在树下一步),并且正在探测父级的代码正在上两步...superview superview。(后一种方法适用于将子视图添加到单元格的 contentView 的 nib 定义的单元格)。

这是一种更安全的爬升视图层次结构的方法,无论单元格是如何构建的,它都可以工作。

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

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

我认为如果您替换此行,您的代码可能会起作用:

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

用这条线

UITableViewCell *cell = [self tableViewCellContaining:imgView]
于 2013-04-05T06:28:17.797 回答
3

我看到你试图访问你的手机,

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

但是您将图像视图添加到

[cell addSubview:myImageView];

您应该将图像视图添加到cell.contentView.

于 2013-04-05T06:33:57.820 回答
1

要获取/知道单击了哪个单元格,请编写以下代码

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

如果你也想得到indexPath窃听的单元格然后写

NSIndexPath *indexPath = [tableView indexPathForCell:cell];

已编辑:-> 另一种方法是

Give tagof UIImageViewin cellForRowAtIndexPathMethod 比如,

imageView.tag = indexPath.row; 

并使用以下代码

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

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

在这里你可以得到CellTapped UIImageView

于 2013-04-05T06:15:03.640 回答