创建 UICollectionViewCell 时如何在发件人中添加标签值?

How can I add tag value in sender while creating an UICollectionViewCell?

我对 swift 非常菜鸟,所以我有一个问题要解决我的应用程序中的一个问题。

在 UIView 中,我刚刚添加了一个集合视图作为子视图,然后在每个单元格中,我在 "Wrapper View" 中添加了不同的图像,所以我的问题是...

我如何添加一个手势来接收每个单元格的发送者的标签值?例如,当我点击单元格时,它会打印 indexPath

我有这个代码:

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


    var cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionCell", forIndexPath: indexPath) as! UICollectionViewCell;

    cell.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0);

    //Agregamos imagen a la celda
    var imageView = UIImageView(frame: CGRectMake(0, 0, cell.frame.width - 0, cell.frame.height - 0))
    var image = UIImage(named: "car_aguas_gerber_7.png")
    imageView.image = image
    cell.backgroundView = UIView()
    cell.backgroundView!.addSubview(imageView)



    // Sección donde creamos el TAP para cada imageView

    // Creamos el tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: "tapGesture:")

    // Se lo adjudicamos al image view previo
    cell.addGestureRecognizer(tapGesture)

    // Nos aseguramos que tenga interacción hacia el usuario
    cell.userInteractionEnabled = true


    cell.tag = indexPath.row;
    println(cell.tag);


    //Una vez creada la celda la regresamos completa.
    return cell;


}

非常感谢您的知识和帮助:)

您只需将手势识别器添加到单元格即可。当手势发生时,传递的参数将是单元格。因此,当声明 tapGesture 方法时,您只需访问发件人的标签 属性.

func tapGesture(sender: UITapGestureRecognizer) {
    var tag = sender.view!.tag
    //do what you want
}