2016-09-23 143 views
0

我有一个ViewController中的UICollection视图,它根本没有响应didSelectItemAtIndexPath。Swift UICollectionView: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) 

     } 

和IB的这个代表。 UiCollectionView delegate in IB

+0

嘿,我想知道是否有在细胞中的任何按钮。或者customCell的内容是什么? –

+0

当我发布问题我没有按钮,只是图像和标签。 现在我已经回答了这个问题,我如何解决这个问题。 去检查我的回答 –

回答

0

几种猜测可能会有所帮助:

  • 做你不小心覆盖- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath

  • 请确保设置collectionView.userInteractionEnabled为true

  • 如果一些高优先级UIResponderUIGestureRecognizerUIButton加入细胞或其子视图,对应的方法应该是调用,而不是

+0

不,我没有这样做,谢谢@陈伟 –

+0

我在同一个viewController中有一个ICarousel,可能是这个原因吗? @陈伟 –

+0

不,您是否将'@IBOutlet collectionview'连接到故事板中的集合视图?如果是这样的话,截图中的名称应该与变量“collectionview”相同,而不是默认名称“Collection View” –

0

我不得不用另一避免这个问题并及时运送我的项目。

我犯了一个按钮我的细胞内和在该方法中“cellForItemAtIndexPath”和添加的目标为它像这样:

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

并且在该按钮标签传递的选定indexPath.row像这样:

imageButton.tag = indexPath.row 

,这我的行动:

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) 

}