在 uitableviewcell 中捕捉 uiimageviewe 触摸 [英] Catch uiimageviewe touch in uitableviewcell

查看:23
本文介绍了在 uitableviewcell 中捕捉 uiimageviewe 触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码不起作用:我从来没有访问过 goToFoodDetail.

I have following code that does not work: I never get to goToFoodDetail.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"<#MyCell#>";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }

    NSInteger objectLocation = indexPath.row;

    UILabel* lblText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 20)];
    [lblText setFont:[UIFont fontWithName:@"Helvetica" size:10.0]];
    [lblText setText:[food foodName]];
    [cell addSubview:lblText];

    UILabel* lblType = [[UILabel alloc] initWithFrame:CGRectMake(0, 21, 260, 20)];
    [lblType setFont:[UIFont fontWithName:@"Helvetica" size:9.0]];
    lblType.textColor = [UIColor blueColor  ];
    [lblType setText:[food foodType]];
    [cell addSubview:lblType];

    UIImageView * detailImage = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"paris.jpg"]] autorelease];
    detailImage.frame = CGRectMake(270, 4, 40, 36);   
    cell.imageView.userInteractionEnabled = YES;
    cell.imageView.tag = indexPath.row;

    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goToFoodDetail:)];
    tapped.numberOfTapsRequired = 1;
    [cell.imageView addGestureRecognizer:tapped];   
    [tapped release];

    [cell addSubview:detailImage];
    [detailImage release];
    [lblText release];
    [lblType release];
    return cell;
}

-(void)goToFoodDetail :(id) sender
{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
    NSLog(@"Tag = %d", gesture.view.tag);
}

推荐答案

看起来您正在创建一个新的 UIImageView 来显示图像 Paris.jp 并将其添加到您的单元格中,然后将点击手势识别器添加到单元格的默认图像视图,但不设置它的图像.您打算在每个单元格中有两个图像视图,还是只有一个?

It looks like you're creating a new UIImageView that shows the image Paris.jp and adding that to your cell, but then adding a tap gesture recognizer to the cell's default image view, but not setting it's image. Are you intending to have two image views in each cell, or just one?

如果您打算让单元格的图像视图既显示图像又可点击,那么您不需要创建另一个图像视图.您可以设置 cell.imageView.image = [UIImage imageWithName:"Paris.jog"] 并将手势识别器附加到相同的位置.

If you intend to have the cell's image view both show the image and be tappable, then you don't need to create the other image view. You can just set cell.imageView.image = [UIImage imageWithName:"Paris.jog"] and attach the gesture recognizer to the same.

这篇关于在 uitableviewcell 中捕捉 uiimageviewe 触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆