简体   繁体   English

通过UIButton选择一个单元格-CollectionViewCell-Swift

[英]Select a cell by UIButton - CollectionViewCell - Swift

I'm using collectionView, and I customized the collectionViewCell in a separate class with type (collectionViewCell). 我正在使用collectionView,并在一个单独的类中将collectionViewCell自定义为类型(collectionViewCell)。 I want to perform an animation when user clicks on a cell. 我想在用户单击单元格时执行动画。 So, I created a UIButton in collectionViewCell and I customized it to cover the whole cell. 因此,我在collectionViewCell中创建了一个UIButton,并对其进行了自定义以覆盖整个单元格。

       button = UIButton(frame: self.frame)
    button.addTarget(self, action: "scaleToSmall", forControlEvents: .TouchDown)
    self.addSubview(button)

Right now, the animation works perfectly, but the issue is I lost the ability select the cell, and the function didSelectItemAtIndexPath does not call anymore 现在,动画效果良好,但是问题是我失去了选择单元格的能力,并且函数didSelectItemAtIndexPath不再调用

I found out how to get the indexPath of the cell that has clicked, 我发现了如何获取已单击的单元格的indexPath,

But how can I call didSelectItemAtIndexPath again and tell it this cell is selected in order to perform the next action (segue to another ViewController) ??? 但是如何再次调用didSelectItemAtIndexPath并告诉它已选择此单元格以便执行下一个操作(选择另一个ViewController)?

Thanks in advance!! 提前致谢!!

You said that you created a button that covers the whole cell, if I have that right. 您说过,如果我有这个权利,那么您创建了一个覆盖整个单元的按钮。 Because of that, didSelectRowAtIndexPath won't do anything because that button is covering the whole cell. 因此,didSelectRowAtIndexPath不会执行任何操作,因为该按钮覆盖了整个单元格。 Take the Twitter app, for example. 以Twitter应用为例。 You can tap on the cell to open the tweet or you can tap on the user's profile image to open their profile. 您可以点击单元格以打开推文,也可以点击用户的个人资料图像以打开其个人资料。 In this case, it's like you have the profile picture button covering the whole cell. 在这种情况下,就像您的个人资料图片按钮覆盖了整个单元格一样。 Maybe you can set the animation in didSelectRowAtIndexPath or make the button smaller. 也许可以在didSelectRowAtIndexPath中设置动画或使按钮变小。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM