【问题标题】:iOS how to know if a row is tapped second time in UITableView?iOS如何知道UITableView中是否第二次点击了一行?
【发布时间】:2012-10-18 10:42:58
【问题描述】:

我正在开发一个 iOS 5 应用程序,在我的情况下,如果用户第一次从 UITableView 中点击一行,那么它将执行一些操作,如果第二次点击它,它将执行不同的操作。

现在,问题是如何知道特定行是否被第二次点击?

提前致谢!!

【问题讨论】:

  • 嗯...将状态存储在单元格或控制器中太明显了,不是吗?
  • @Cfr 嘿伙计,这不是那个问题的重复。我对双击不感兴趣,而是关于如果用户点击一行,并且在一段时间后(他可能会选择另一行),如果他再次点击该行,那么它将调用不同的操作。
  • 哦,对不起。无论如何,您可以将该解决方案移植到您的解决方案
  • 谢谢@Eiko,我会试试你的建议,希望它对我有用。

标签: objective-c ios5 uitableview


【解决方案1】:

那你为什么不将 UIGestures 与 TableView 一起使用。这将是最简单的解决方案,对我来说就像魅力一样。

以下是帮助您在代码中实现它的代码:

    // Put this code in your ViewDidLoad:
    UITapGestureRecognizer *doublegesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTableViewCellDoubleTapping:)];
    doublegesture.numberOfTapsRequired = 2;
    [roomTableView addGestureRecognizer:doublegesture];
    [doublegesture release];

//双击事件的处理方法

-(void)didTableViewCellDoubleTapping:(UITapGestureRecognizer *)gestureRecognizer 
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint swipeLocation = [gestureRecognizer locationInView:roomTableView];
        NSIndexPath *swipedIndexPath = [roomTableView indexPathForRowAtPoint:swipeLocation];
        NSLog(@"%d",swipedIndexPath.row);
                
        // ... Here you can add your logic
    }
}

以类似的方式,如果您为单击添加另一个 UIGesture

 UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTableViewSingleCellTapping:)];
    gesture.numberOfTapsRequired = 1;
    [roomTableView addGestureRecognizer:gesture];
    [gesture release];
    
    [gesture requireGestureRecognizerToFail:doublegesture];

-(void)didTableViewSingleCellTapping:(UITapGestureRecognizer *)gestureRecognizer 
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        CGPoint swipeLocation = [gestureRecognizer locationInView:roomTableView];
        NSIndexPath *swipedIndexPath = [roomTableView indexPathForRowAtPoint:swipeLocation];
        
        NSLog(@"%d",swipedIndexPath.row);
    }
}

【讨论】:

    【解决方案2】:

    这是我的解决方案(正如 Eiko 建议的那样):

    每当第一次在数组中选择行并调用第一个操作时,维护一个可变数组并存储行索引。当第二次选择行或选择另一行时,检查该行的索引是否存在于数组中,如果是则调用第二个操作,否则将行的索引添加到数组并调用第一个操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2014-12-13
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多