2011-10-12 12 views
27

現在、UIImageViewを含むカスタムUITableViewCellがあり、UIImageViewにUIImageViewを追加することはできません。ここにコードの抜粋です。UITableVellWCell内のUIImageViewのUITapGestureRecognizerが呼び出されない

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused) 
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)]; 
tap.cancelsTouchesInView = YES; 
tap.numberOfTapsRequired = 1; 
tap.delegate = self; 
[imageView addGestureRecognizer:tap]; 
[tap release]; 

// handle method 
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer { 
    RKLogDebug(@"imaged tab"); 
} 

私はまた、細胞とUIImageViewのスーパーが、まだ運、任意のヒントにuserInteractionEnabled設定していますか?

EDIT

私もcell.selectionStyle = UITableViewCellSelectionStyleNoneにより、セルの選択を解除しました。これは問題である可能性があります

+0

まず、画像ビューの代わりにUIButtonを使用していないのはなぜですか?第二に、実際の画像ビューでユーザーのやりとりを有効にしましたか? – Rog

+0

@Rog 'cus UIImageViewのUIViewContentModeScaleAspectFit関数を使用したいのですが、UIButtonに同じ機能がありますか? – Herman

+0

また、UIControlをスクロールビューで使用すると、タッチが始まるとスクロールがブロックされます。 – JakubKnejzlik

答えて

98

UIImageViewのユーザの操作はデフォルトでは無効にされていますが無効になっています。タッチに反応させるには、明示的に有効にする必要があります。私の場合は

imageView.userInteractionEnabled = YES; 
+2

私はまだそれをやったことがありますが、まだ運がありません – Herman

+0

は私のために働いた...ありがとう!! – samfisher

+1

多分あなたはその上にUIViewを持っていますか? –

0

は、それは次のようになります。

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER; 
    RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) { 
     cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:cellIdentifier]; 
    } 

    if ([self.routes count] > 0) { 
     Route *route = [self.routes objectAtIndex:indexPath.row]; 

     UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self 
                        action:@selector(imageOwnerTapped:)]; 
     singleTapOwner.numberOfTapsRequired = 1; 
     singleTapOwner.cancelsTouchesInView = YES; 
     [cell.ownerImageView setUserInteractionEnabled:YES]; 
     [cell.ownerImageView addGestureRecognizer:singleTapOwner]; 
    } else { 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 
    return cell; 
} 

セレクタ:

- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture { 
    CGPoint location = [gesture locationInView:self.tableView]; 
    NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location]; 
    UITableViewCell *tapedCell = [self.tableView cellForRowAtIndexPath:tapedIndexPath]; 

    NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell]; 
    NSUInteger index = [indexPath row]; 

    Route *route = [self.routes objectAtIndex:index]; 
} 
2

スウィフト3

これは私の仕事:

self.isUserInteractionEnabled = true 
関連する問題