簡體   English   中英

didSelectItemAtIndexPath在集合視圖Swift中不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM