如何检测 UITableView 中某个单元格的双击? [英] How can I detect a double tap on a certain cell in UITableView?

查看:31
本文介绍了如何检测 UITableView 中某个单元格的双击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测对 UITableView 中某个单元格的双击?

How can I detect a double tap on a certain cell in UITableView?

我想在用户进行单次触摸时执行一个操作,如果用户进行两次触摸,我想执行另一个操作?我还需要知道进行触摸的索引路径.

i.e. I want to perform one action if the user made a single touch and another if a user made a double touch? I also need to know an index path where the touch was made.

我怎样才能实现这个目标?

How can I achieve this goal?

谢谢.

推荐答案

如果您不想创建 UITableView 的子类,请使用带有 table view 的 didSelectRowAtIndex: 的计时器代码>

If you do not want to create a subclass of UITableView, use a timer with the table view's didSelectRowAtIndex:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //checking for double taps here
    if(tapCount == 1 && tapTimer != nil && tappedRow == indexPath.row){
        //double tap - Put your double tap code here
        [tapTimer invalidate];
        [self setTapTimer:nil];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Double Tap" message:@"You double-tapped the row" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
        [alert release];
    }
    else if(tapCount == 0){
        //This is the first tap. If there is no tap till tapTimer is fired, it is a single tap
        tapCount = tapCount + 1;
        tappedRow = indexPath.row;
        [self setTapTimer:[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(tapTimerFired:) userInfo:nil repeats:NO]];
    }
    else if(tappedRow != indexPath.row){
        //tap on new row
        tapCount = 0;
        if(tapTimer != nil){
            [tapTimer invalidate];
            [self setTapTimer:nil];
        }
    }
}

- (void)tapTimerFired:(NSTimer *)aTimer{
    //timer fired, there was a single tap on indexPath.row = tappedRow
    if(tapTimer != nil){
        tapCount = 0;
        tappedRow = -1;
    }
}

HTH

这篇关于如何检测 UITableView 中某个单元格的双击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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