未调用 UIScrollView 内的 UICollectionView 的 didSelectItemAtIndexPath
didSelectItemAtIndexPath of UICollectionView inside of a UIScrollView is not getting called
我已经在 UIscrollview 中实现了 不可滚动的 UICollectionView。
滚动视图尺寸为100x100,集合视图尺寸为100x200;
滚动视图的内容大小为 100x200。
我的问题是,当我触摸某些单元格(100x100 矩形以外的单元格)时,didSelectItemAtIndexPath 没有被调用。
单元格的用户交互已启用。当我将滚动视图的高度增加到等于集合视图的高度时,所有单元格都是可触摸的。
提前致谢。
因为 scrollView 在 Cell 上重叠...最好的方法是在 UIScrollView 上添加点击手势,例如,
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
self.scrollViu.userInteractionEnabled = YES;
[self.scrollViu addGestureRecognizer:recognizer];
在 cellForItemAtIndexPath 方法中添加以上代码并编写手势动作方法,例如
-(void)gestureAction:(UITapGestureRecognizer *) sender
{
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.YourCollectionViewName];
NSIndexPath *indexPath = [self.YourCollectionViewName indexPathForRowAtPoint:touchLocation];
NSLog(@"%d", indexPath.item);
}
这里在上面的手势(action)方法中可以得到indexPath,和didSelectItemAtIndexPath一样。
我的建议是采用一个大小与集合视图相同的容器,即 100x200,然后在容器上添加集合视图,然后在滚动视图上添加容器。这应该可以解决此问题。
因为我遇到了类似的问题,因为在滚动视图的不可见部分进行交互时,我无法滚动集合视图和 select 集合视图。
Swift 4.0
在viewDidLoad函数中注册手势
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView(gesture:)))
view.addGestureRecognizer(tapGesture)
}
@objc func didTapView(gesture: UITapGestureRecognizer) {
view.endEditing(true)
let touchLocation:CGPoint = gesture.location(ofTouch: 0, in: self.collectionView)
let indexPath = self.collectionView.indexPathForItem(at: touchLocation)
if indexPath != nil {
let cell = self.collectionView.cellForItem(at: indexPath!)
if (cell?.isSelected)! {
//PREFORM DESELECT
} else {
//PREFORM SELECT HERE
}
}
}
我已经在 UIscrollview 中实现了 不可滚动的 UICollectionView。 滚动视图尺寸为100x100,集合视图尺寸为100x200; 滚动视图的内容大小为 100x200。
我的问题是,当我触摸某些单元格(100x100 矩形以外的单元格)时,didSelectItemAtIndexPath 没有被调用。
单元格的用户交互已启用。当我将滚动视图的高度增加到等于集合视图的高度时,所有单元格都是可触摸的。 提前致谢。
因为 scrollView 在 Cell 上重叠...最好的方法是在 UIScrollView 上添加点击手势,例如,
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
[recognizer setNumberOfTapsRequired:1];
self.scrollViu.userInteractionEnabled = YES;
[self.scrollViu addGestureRecognizer:recognizer];
在 cellForItemAtIndexPath 方法中添加以上代码并编写手势动作方法,例如
-(void)gestureAction:(UITapGestureRecognizer *) sender
{
CGPoint touchLocation = [sender locationOfTouch:0 inView:self.YourCollectionViewName];
NSIndexPath *indexPath = [self.YourCollectionViewName indexPathForRowAtPoint:touchLocation];
NSLog(@"%d", indexPath.item);
}
这里在上面的手势(action)方法中可以得到indexPath,和didSelectItemAtIndexPath一样。
我的建议是采用一个大小与集合视图相同的容器,即 100x200,然后在容器上添加集合视图,然后在滚动视图上添加容器。这应该可以解决此问题。
因为我遇到了类似的问题,因为在滚动视图的不可见部分进行交互时,我无法滚动集合视图和 select 集合视图。
Swift 4.0
在viewDidLoad函数中注册手势
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapView(gesture:)))
view.addGestureRecognizer(tapGesture)
}
@objc func didTapView(gesture: UITapGestureRecognizer) {
view.endEditing(true)
let touchLocation:CGPoint = gesture.location(ofTouch: 0, in: self.collectionView)
let indexPath = self.collectionView.indexPathForItem(at: touchLocation)
if indexPath != nil {
let cell = self.collectionView.cellForItem(at: indexPath!)
if (cell?.isSelected)! {
//PREFORM DESELECT
} else {
//PREFORM SELECT HERE
}
}
}