31

現在、UIImageView を含むカスタム UITableViewCell があり、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 を設定しましたが、まだ運がありません。ヒントはありますか?

編集

cell.selectionStyle = UITableViewCellSelectionStyleNone; によってセルの選択も削除しました。これは問題になるのでしょうか

4

3 に答える 3

107

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

imageView.userInteractionEnabled = YES;
于 2011-10-12T04:52:27.853 に答える
5

スイフト3

これは私のために働いた:

self.isUserInteractionEnabled = true
于 2017-02-08T21:07:14.497 に答える
2

私の場合、次のようになります。

- (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];
}
于 2015-07-20T21:36:25.677 に答える