UITableViewCell中的UIScrollView – 没有didSelect调用

我有一个tableviewCell ,用户可以水平scroll 。 由于scrollView几乎覆盖了整个cell ,因此如果用户单击cell cell ,则不会调用tableView方法didSelectRow

所以我想,我可以将UIScrollView的触摸事件传递给cell ,但didSelectRow仍然不被调用。 我分类UIScrollView只传递触摸事件,如果触摸不是一个拖动:

 - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event { NSLog(@"touch scroll"); // If not dragging, send event to next responder if (!self.dragging) [self.superview touchesEnded: touches withEvent:event]; else [super touchesEnded: touches withEvent: event]; } 

有关如何将点击传递到表的任何想法,以获得委托方法调用,并保持滚动内滚动scrollview

你可以实际做到这一点,而不需要UIScrollView 。 无论您有自定义单元格,还是在UITableView cellForRowAtIndexPath中设置属性,都可以执行以下操作:

 [cell.contentView addSubview:yourScrollView]; yourScrollView.userInteractionEnabled = NO; [cell.contentView addGestureRecognizer:yourScrollView.panGestureRecognizer]; 

你可以做到这一点的原因是因为scrollView有自己的panGestureRecognizer可供程序员访问。 所以,只要将它添加到单元格的视图将触发滚动视图的手势代表。

这种方法唯一的缺点是滚动视图的子视图不能接收任何触摸input。 如果你需要这个,你将不得不select一个不同的方法。

我刚刚遇到同样的问题。
在你的子类中,确保包含一整套方法:

 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.dragging) [self.superview touchesCancelled: touches withEvent:event]; else [super touchesCancelled: touches withEvent: event]; } -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.dragging) [self.superview touchesMoved: touches withEvent:event]; else [super touchesMoved: touches withEvent: event]; } -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.dragging) [self.superview touchesBegan: touches withEvent:event]; else [super touchesBegan: touches withEvent: event]; } -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (!self.dragging) [self.superview touchesEnded: touches withEvent:event]; else [super touchesEnded: touches withEvent: event]; } 

尝试设置此

 _scrollView.canCancelContentTouches = NO 

另外,部分转发触摸事件也是不好的

所选的答案是正确的,但我更新了代码基于我得到的错误。

 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (self.dragging) { [super touchesMoved:touches withEvent:event]; } else { if ([self.delegate isKindOfClass:[UITableViewCell class]]) { [(UITableViewCell *)self.delegate touchesCancelled:touches withEvent:event]; } [self.superview touchesMoved:touches withEvent:event]; } } 

如果你的self.delegate不是UITableViewCell ,那么用属性replace属性到你的单元格。

移动过程中,单元需要检索取消触摸事件,以防止出现不希望的结果。 它可以很容易重现如下。

  • 突出显示单元格(假设滚动视图覆盖整个单元格,如果不突出显示滚动视图)
  • 单元格突出显示时,拖动表格视图
  • select任何其他单元格,现在先前突出显示的单元格将检索didSelectCell状态

还有一点需要提到的是订单很重要! 如果在self.superview之前没有调用self.delegate ,那么突出显示的状态就不会发生。

Swift 3

 scrollView.isUserInteractionEnabled = false contentView.addGestureRecognizer(scrollView.panGestureRecognizer)