未在swift中调用中的didSelectItemAtIndexPath

未在swift中调用中的didSelectItemAtIndexPath,swift,ios8,Swift,Ios8,我有一个集合视图,其中包含图像视图和每个单元格中的按钮,我在其中单击图像视图时会调用didSelectItemAtIndexPath,而单击按钮时不会调用它 这是代码 override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = colle

我有一个集合视图,其中包含图像视图和每个单元格中的按钮,我在其中单击图像视图时会调用didSelectItemAtIndexPath,而单击按钮时不会调用它

这是代码

       override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserCellIdentifier, forIndexPath: indexPath) as! PhotoBrowserCollectionViewCell
    let sharedImageCache = FICImageCache.sharedImageCache()
    cell.imageView.image = nil
    let button   = UIButton.buttonWithType(UIButtonType.System) as! UIButton
    button.frame = CGRectMake(90, 90, 15, 15)
    button.backgroundColor = UIColor.greenColor()

    //cell.contentView.addSubview(button)
    let photo = photos[indexPath.row] as PhotoInfo
    if (cell.photoInfo != photo) {

        sharedImageCache.cancelImageRetrievalForEntity(cell.photoInfo, withFormatName: formatName)

        cell.photoInfo = photo

       // cell.contentView.addSubview(button)

    }
    sharedImageCache.retrieveImageForEntity(photo, withFormatName: formatName, completionBlock: {
        (photoInfo, _, image) -> Void in
        if (photoInfo as! PhotoInfo) == cell.photoInfo {
           // cell.imageView.image = image
            var imgView = UIImageView(image:image!)
            imgView.frame = CGRectMake(0,0,cell.frame.size.width, cell.frame.size.height);
            cell.contentView.addSubview(imgView)

            cell.contentView.addSubview(button)
        }
    })

    return cell
}


  override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let photoInfo = photosVideo[indexPath.row]
    var url = photoInfo.sourceImageURL
           button.addTarget(self, action: "showNotice", forControlEvents: UIControlEvents.TouchUpInside)
    performSegueWithIdentifier("show photo", sender: ["photoInfo": photoInfo])
    println("hello")
}
尝试替换此选项:

button.addTarget(self, action: "showNotice", forControlEvents: UIControlEvents.TouchUpInside)
为此:

button.addTarget(self, action: "showNotice:", forControlEvents: UIControlEvents.TouchUpInside)

didSelectItemAtIndexPath
在您单击按钮时将不会调用,因此您必须以不同的方式进行操作,如我在下面提到的:

  • cellForItemAtIndexPath
    中创建按钮时,只需通过以下方式添加一个标记:

    button.tag = indexPath.row
    
  • 之后,您必须添加在按下按钮时调用的操作:

    button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
    
  • 之后,当您将按钮按入单元格时,可以调用此方法:

    func buttonTapAction(sender:UIButton!)
    {
        var button : UIButton = sender as! UIButton
        var row : Int = button.tag
    
        println(row)
    }
    
  • 因此,通过这种方式可以获得
    indepath
    的值


    有关更多参考,您可以参考使用tableview的示例,但它将帮助您更好地理解

    按钮.addTarget(self,action:“showNotice”,forControlEvents:UIControlEvents.TouchUpInside)
    是否创建了showNotice功能
    func showNotice(发送者:UIButton!){println(“这里的按钮”)}
    为什么要在didSelectItemAtIndexPath的按钮中添加目标?当您在cellForItemAtIndexPath中创建按钮时,需要创建操作,并且(我在这里不太确定,因为我不确定您想要做什么)但是我相信你不需要didSelectItemAtIndexPath,因为当你触摸按钮而不是视图时,会发生一些事情。我会这样做,但它不起作用。我想使用indexPath的值,所以我在didSelectItemAtIndexPath中添加按钮目标。我的问题是当我单击按钮时,在单元格中单击它,didSelectItemAtIndexPath方法没有callHi,我们知道其他修复方法,但是集合视图公共属性知道cellForItemAtIndexPath吗?这不是电话,请让我知道为什么,现在我也面临同样的问题。