简体   繁体   English

Swift UICollectionView:didSelectItemAtIndexPath没有响应

[英]Swift UICollectionView : didSelectItemAtIndexPath not responding

i have UICollection view in a ViewController and it's not responding to didSelectItemAtIndexPath at all. 我在ViewController中有UICollection视图,它根本不响应didSelectItemAtIndexPath。

// super class
class ViewController: UIViewController, iCarouselDelegate, iCarouselDataSource, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!


// delegate
override func viewDidLoad() {
        super.viewDidLoad()

        // collection view delegate and datasource
        collectionView.delegate = self
        collectionView.dataSource = self


// did select item
        func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!) {

           print(indexPath)

        }

and this delegate from IB . IB的这位代表。 IB中的UiCollectionView委托

several guesses might be helpful: 一些猜测可能会有所帮助:

  • did you accidentally overriding - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath ? 您是否意外覆盖- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath吗?

  • make sure set collectionView.userInteractionEnabled to true 确保将collectionView.userInteractionEnabled设置为true

  • if some high priority UIResponder like UIGestureRecognizer and UIButton added to cell or its subview, corresponding method should be call instead 如果将某些高优先级的UIResponderUIGestureRecognizerUIButton添加到单元格或其子视图中,则应调用相应的方法

i had to use another way to run away from this issue and ship my project in time. 我不得不使用另一种方式来解决这个问题,并及时交付我的项目。

i made a button inside my cell and in this method "cellForItemAtIndexPath" and added target for it like so: 我在单元格内创建了一个按钮,并在此方法“ cellForItemAtIndexPath”中添加了目标,如下所示:

imageButton.addTarget(self, action: #selector(ViewController.imageButtonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)

and passed the selected indexPath.row in this button tag like so : 并在此按钮标签中传递选定的indexPath.row,如下所示:

imageButton.tag = indexPath.row

and this my Action : 这是我的行动:

func imageButtonAction(sender: UIButton) {

let categoryId = categories[sender.tag]["id"]
    let categoryName = categories[sender.tag]["category_name"]

    let mainCategory = self.storyboard?.instantiateViewControllerWithIdentifier("MainCategoryViewController") as! MainCategoryViewController
    mainCategory.mainCategoryId = categoryId!
    mainCategory.title = categoryName!
    self.navigationController!.pushViewController(mainCategory, animated: true)

}

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

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