簡體   English   中英

檢測UITableViewCell子視圖內的觸摸

[英]Detecting touch inside UITableViewCell subview

我不清楚在哪里應該將UIGestureRecognizer代碼添加到UITableViewCell的相應子視圖中。 我已經閱讀了所有可以找到的相關問題。 現在我的cell和cell的子視圖在cellForRowAtIndexPath中生成。 我試圖用cellForRowAtIndexPath添加Gesture:

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[mySubview addGestureRecognizer:tapGesture];
tapGesture.cancelsTouchesInView = YES;
tapGesture.delegate = self;

但是,這沒有任何檢測。 為了驗證我的UIGesture識別器是否正常工作,我在tableView本身上使用了上面的代碼,它確實按預期注冊了觸摸。 此外,當tableView附加了上述手勢時,下面的代碼也按預期調用:

-(BOOL) gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    NSLog(@"shouldRevceiveTouch");
    return YES;
}

- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UITapGestureRecognizer *)otherGestureRecognizer 
{
    NSLog(@"simultaneously");
    return YES;
}

我試圖從tableView和cellForRowAtIndexPath中刪除GestureRecognizer我試圖將GestureRecognizer附加到單元格本身,任何子視圖,沒有其他任何東西檢測到觸摸。 (以上代碼均未觸發)

顯然我錯誤地添加了GestureRecognizer。 何時/何時是添加GestureRecognizer的合適位置/時間?

謝謝。

我做過類似的事情,但它是UILongPressGestureRecognizer 我認為沒有太大的區別(因為所有的觸摸都是由UITableView接收的)。 我在控制器viewDidLoad方法(NOT IN單元格)中添加了手勢識別器。

- (void) tableViewLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.messageTableView];

    NSIndexPath *indexPath = [self.messageTableView indexPathForRowAtPoint:p];
    if (indexPath == nil)
        NSLog(@"long press on table view but not on a row");
    else {
        UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
        CGPoint pointInCell = [cell convertPoint:p fromView:self.messageTableView];
    }
}

您可以將長按更改為常規按並自行嘗試

我需要檢測單元格內不同子視圖的觸摸。 還處理iOS 9的UITableViewCellContentView

首先,我在我的自定義UITableViewCell中覆蓋了touchesBegan

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:touch.view];

    // Imagine I have 2 labels inside my cell
    CGPoint convertedPoint = [self.firstLabel convertPoint:point fromView:touch.view];
    if ([self.firstLabel pointInside:convertedPoint withEvent:nil]) {
        // Touched first label
        return;
    } 

    convertedPoint = [self.secondLabel convertPoint:point fromView:touch.view];
    if ([self.secondLabel pointInside:convertedPoint withEvent:nil]) {
        // Touched second label
        return;
    } 

    // no labels touched, call super which will call didSelectRowAtIndexPath
    [super touchesBegan:touches withEvent:event];
}

要修復iOS 9中的支持,我們應該覆蓋awakeFromNib,或者如果單元格不在Storyboard / xib中,則只禁用單元格用戶交互。

- (void)awakeFromNib {
    // Initialization code
    self.contentView.userInteractionEnabled = NO;
} 

當然,我們不應該忘記設置我們的標簽用戶交互啟用。

不確定你到底想要做什么。 如果您只想檢測用戶是否點擊了表格中的單元格,那么您不需要實現手勢識別器。 只需實現下面的委托方法來檢測何時選擇了表中的 ,然后處理行的元素,例如獲取子視圖等。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   // Do all my cool tap related stuff here for example, get the row that was tapped:
   UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
   // get your subview (assume its a UIImageView) from cell - one way to do it below
   UIImageView photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG];
}

如果你進一步描述你的問題,那么也許我可以提供額外的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM