如何在创建UICollectionViewCell时在发件人中添加标记值? [英] How can I add tag value in sender while creating an UICollectionViewCell?

查看:129
本文介绍了如何在创建UICollectionViewCell时在发件人中添加标记值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm very noob with swift, so i Have a question for solve a trouble in my app.

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

In a UIView i have just added a Collection View as a Sub View, then in each cell I added a different IMAGE inside a "Wrapper View", so my question is...

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

How can i add a gesture that receives the tag value by a sender for each cell? For example, when i tap the cell it'll print the 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;


}

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

Thanks a lot in advance for your knowledge and help :)

推荐答案

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

You just have it when adding the gesture recognizer to the cell. When the gesture happens, the parameter passed will be the cell. So when declaring the tapGesture method you just access the sender's tag property.

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

这篇关于如何在创建UICollectionViewCell时在发件人中添加标记值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆