从索引路径获取索引整数从Swift中的单元格获取

Che*_*lme 3 ios swift swift2

这是两个功能.

第一个功能是我给我的每个单元格做手势UICollectionView.

我试图在发生双击手势事件时将indexPath从此函数发送到我的第二个函数.所以我的行动电话doubleTap:.

在我的第二个函数中,我试图使用发送者获取indexPath单元格.当我双击第一个单元格时,我能够得到一个indexPath,当记录时,打印出"0xc000000000078016> {length = 2,path = 0 - 0}".

当我点击第一个单元格时,如何得到整数0?

欢迎所有建议.谢谢大家.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as myViewCell

    //adding single and double tap gestures for each cell
    /////////////////////////////
    //ISSUE IS SENDING indexPath TO doubleTap FUNC
    var gestureDoubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTap:")
    gestureDoubleTap.numberOfTapsRequired = 2
    cell.addGestureRecognizer(gestureDoubleTap)

    var gestureSingleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "singleTap")
    gestureSingleTap.numberOfTapsRequired = 1
    cell.addGestureRecognizer(gestureSingleTap)

    cell.imgView.image=UIImage(named: imageArray[indexPath.row])

    return cell
}

func doubleTap(sender: UITapGestureRecognizer) {
    var tapLocation = sender.locationInView(self.collectionView)
    var indexPath:NSIndexPath = self.collectionView.indexPathForItemAtPoint(tapLocation)!

    //var cell = self.collectionView.cellForItemAtIndexPath(indexPath)

    NSLog("double tap")
    NSLog("%@", indexPath)

    //NSLog("%@", cell!)
    //name = imageArray[indexPath]
    //self.performSegueWithIdentifier("segue", sender: nil)
}
Run Code Online (Sandbox Code Playgroud)

Unh*_*lig 11

您可以使用以下内容index从触摸点检索,如代码所示:

var indexPath : NSIndexPath = self.collectionView.indexPathForItemAtPoint(tapLocation)!

let rowNumber : Int = indexPath.row

//use rowNumber as index for your array.
Run Code Online (Sandbox Code Playgroud)

要么:

let index : Int = (indexPath.section * maxCol) + indexPath.row
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@Unheilig!这帮助我检索了索引! (2认同)

Mun*_*ndi 6

假设您没有使用您获得项目编号的部分

indexPath.item
Run Code Online (Sandbox Code Playgroud)