![](/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.