![](/img/trans.png)
[英](Swift) didSelectItemAtIndexPath in UICollectionView defined in external view doesn't work
[英]didSelectItemAtIndexPath Doesn't Work In Collection View Swift
我一直在开发一个新应用程序,它在“收藏夹视图”中显示Gif。 我还在集合视图中的单元格使用自定义集合视图单元格类。
方法didSelectItemAtIndexPath
无效,但是...
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("it worked")
// ^this did not print
}
我将如何更改它,以便可以使用手势识别器获取被单击项的indexPath?
正如@santhu所说( https://stackoverflow.com/a/21970378/846780 )
当collectionViewCell的subView都不响应该触摸时,将调用didSelectItemAtIndexPath。 由于textView响应这些触摸,因此不会将这些触摸转发到其superView,因此collectionView将无法获得它。
因此,您有一个UILongPressGestureRecognizer
,它避免了didSelectItemAtIndexPath
调用。
使用UILongPressGestureRecognizer
方法,您需要处理handleLongPress
委托方法。 基本上,您需要获取gestureReconizer.locationInView
并知道此时的indexPath( gestureReconizer.locationInView
)。
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.Ended {
return
}
let p = gestureReconizer.locationInView(self.collectionView)
let indexPath = self.collectionView.indexPathForItemAtPoint(p)
if let index = indexPath {
var cell = self.collectionView.cellForItemAtIndexPath(index)
// do stuff with your cell, for example print the indexPath
println(index.row)
} else {
println("Could not find index path")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.