1

這是我的代碼:如何使用Swift中的UILongPressGestureRecognizer通過tag屬性傳遞indexPath.row?

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! imageUserCell 

    let coolGesture = UILongPressGestureRecognizer(target: self, action: #selector(detailedPerson.makeItCoolAction(_:))) 

    coolGesture.minimumPressDuration = 0.5 
    coolGesture.view?.tag = 4 

    cell.addGestureRecognizer(coolGesture) 

這是函數代碼:

func makeItCoolAction(sender: UIGestureRecognizer){ 

    print(sender.view?.tag) 
    print("Photo cooled") 

} 

控制檯打印相同的值:0,這是默認的。我強烈地需要將indexPath.row值傳遞給makeItCoolAction,但我看到UIGestureRecognizer沒有標籤屬性,但.view屬性具有它,就像您在代碼中看到的那樣。

我已將makeItCoolAction發件人從UILongPressGestureRecognizer更改爲UIGestureRecogizer,並且它仍然保持打印0值。

回答

0

的看法是零,在這一點上:

coolGesture.view?.tag = 4 

嘗試設置水龍頭,你已經添加了手勢之後,它有這樣的觀點:

cell.addGestureRecognizer(coolGesture) 
coolGesture.view?.tag = 4 
+0

你該死的權利!有用! –

相關問題