在iOS中的UITableviewCell子视图中触摸Scrollview的事件

时间:2012-06-28 08:50:54

标签: iphone ios xcode uitableview uiscrollview

我曾尝试在互联网上搜索此问题,但无法提出解决我问题的解决方案。

我已经创建了UITableViewCell的子类,其中我添加了UIScrollView作为子视图,现在我希望滚动手势由scrollView监听,单击手势由UItableView监听。
同样使用此布局是针对Apple的HIG

2 个答案:

答案 0 :(得分:2)

这是一个老问题,但由于我没有找到一个好的答案,我认为我提供了一个。这种方法捕获UIScrollView上的tap事件(包含'表格单元格内)和直接点击UITableView单元格上的事件,并将它们传递给UITableViewDelegate。它还允许您滚动UIScrollView内容而不传递滚动事件。不需要任何子类化。

  1. 在ViewDidLoad中,在UITableView上实例化UITapGestureRecognizer
  2. // Capture taps on scrollView fields and pass along to tableView
    let tap = UITapGestureRecognizer(target: self, action: "tapIntercept:")
    tableView.addGestureRecognizer(tap)
    
    1. 为上面的选择器
    2. 创建一个回调函数
      // Get the location of the table cell beneath the scrollView and pass the event off to the tableView
      func tapIntercept(recognizer: UIGestureRecognizer) { 
          let point = recognizer.locationInView(tableView)
          if let indexPath = tableView.indexPathForRowAtPoint(point) {
              tableView(tableView, didSelectRowAtIndexPath: indexPath)
          }  
      }
      

答案 1 :(得分:0)

如果滚动视图是表格视图的子视图,您可以捕获手势并将其发送到其父视图,如下所示:

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.nextResponder touchesBegan:touches withEvent:event];
    }