Ios 在创建UICollectionViewCell时,如何在sender中添加标记值?
我对swift很在行,所以我有一个问题要解决我应用程序中的一个问题 在UIView中,我刚刚添加了一个集合视图作为子视图,然后在每个单元格中,我在“包装器视图”中添加了一个不同的图像,所以我的问题是 如何添加一个手势,让发送者接收每个单元格的标记值?例如,当我点击单元格时,它将打印索引 我有以下代码:Ios 在创建UICollectionViewCell时,如何在sender中添加标记值?,ios,iphone,xcode,swift,ipad,Ios,Iphone,Xcode,Swift,Ipad,我对swift很在行,所以我有一个问题要解决我应用程序中的一个问题 在UIView中,我刚刚添加了一个集合视图作为子视图,然后在每个单元格中,我在“包装器视图”中添加了一个不同的图像,所以我的问题是 如何添加一个手势,让发送者接收每个单元格的标记值?例如,当我点击单元格时,它将打印索引 我有以下代码: func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPat
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;
}
提前非常感谢您的知识和帮助:)将手势识别器添加到手机时,您就可以使用它了。当手势发生时,传递的参数将是单元格。因此,在声明TapPassive方法时,只需访问发送者的标记属性
func tapGesture(sender: UITapGestureRecognizer) {
var tag = sender.view!.tag
//do what you want
}
通过UICollectionView委托方法
-(void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
,并在此方法中调用[collectionView cellForItemAtIndexPath:indexPath]代码>返回抽头单元格。要获取它的标签,只需获取它的标签属性感谢您的帮助,但我对这种语言非常陌生…它是否快速?谢谢!!我已经使用过它,但当我点击单元格时,它会标记以下错误:2015-06-19 09:39:47.322 iNesbook[2608:37458]-[UITapgestureRecognitizer tag]:未识别的选择器发送到实例0x7f9b38f3f580 2015-06-19 09:39:47.435 iNesbook[2608:37458]***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[UITapGestureRecognitor标记]:发送到实例0x7f9b38f3f580'***第一次抛出调用堆栈的无法识别的选择器:(…)libc++abi.dylib:以NSException类型的未捕获异常终止我的声明有什么问题吗?抱歉,发件人是UITapGestureRecognizer类型的,因此您必须访问附加到它的视图并获取其标记,我将更新应答谢谢!!它工作起来很有魅力!!!!!但为了获得集体知识,请使用swi中的正确语法ft是以下语句:func-tappership(发送方:UITapGestureRecognizer){println(sender.view!.tag);}只需添加“!”符号;)