在CustomCell中检测UIImageView上的触摸

时间:2012-10-02 08:57:28

标签: ios uitableview uiimageview uigesturerecognizer

我有自定义UITableViewCell,我已经将UITableViewCell子类化了:

MyCustomCell.h

MyCustomCell:UITableViewCell

然后我还有一个这个自定义单元格的xib文件,一切正常,我可以显示所有信息,以及我在单元格上添加的图像,但我想在用户触摸uiimageview时检测到触摸,所以我试过这样的方式:

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

static NSString *CellIdentifier = @"MasterView";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[MasterViewCell alloc] init];
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MasterViewCustomCellImage" owner:self options:nil];
    cell = (MasterViewCell *)[nib objectAtIndex:0];
    //NSLog(@"Nuova Cella");
}

[self configureCell:cell atIndexPath:indexPath];

return cell;
}

 - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
 {
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];

UIImageView *thumbnailImage = (UIImageView *)[cell viewWithTag:1007];
[thumbnailImage setImage:[managedObject valueForKey:@"myImage"]];

if (thumbnailImage) {

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(detectTouchImage)];
    [longPress setMinimumPressDuration:1.0];
    [thumbnailImage setUserInteractionEnabled:YES];
    [thumbnailImage addGestureRecognizer:longPress];
}

 }

-(void)detectTouchImage
{
 NSLog(@"Image Pressed");
 }

但我无法理解为什么不工作,它输入thumbnailImage if,但是没有检测到图像上的任何手势......任何人都可以帮助我吗?我在ios 5和ios 6上试过了,但是没有用......

2 个答案:

答案 0 :(得分:0)

问题很可能是细胞劫持了触摸。你可以检查一下你的didSelectCellForRow方法是否被调用,或者在点击它时突出显示该行?还尝试将UILongPressGestureRecognizer分配给单元格而不是图像。 如果这样做但你只想在点击拇指图像时调用点击,你可以使用locationInView类中实现的UIGestureRecognizer方法来查看用户点击的确切位置。

答案 1 :(得分:-1)

刚刚制作测试项目以验证问题所在 完整的工作示例是GitHub上的here。下载并玩它。 TableViewCell具有附加UILongPressGestureRecognizer的自定义UIImageView。一切都很好。长按任何图像并弹出UIIAllertView。所以,使用UILongPressGestureRecognizer的代码似乎没问题。问题出现在项目的其他部分,您不会暴露。