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)
- 为什么当我的UITableView尝试加载时出现无法出队的错误?
- 点击时,UICollectionView细胞改变背景
- 如何在多个UIViews中滑动手指时检测特定UIView中的触摸
- 如何在两个ViewController之间传递数据,其中第一个VC有两个tableViews,第二个tableViewselect的数据应该传递给第二个VC?
- 尝试使用custon UITableViewCell时发生错误“此类不是键值编码兼容的密钥…”
- 平衡多行UILabel
- UICollectionView的cellForItemAtIndexPath没有被调用
- 通过类别覆盖Objective c中的方法
- 保存希伯来语文本NSUserDefaults返回奇怪的编码