2016-09-23 142 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) 

}