2016-10-12 21 views
0

後叫我有一個簡單的集合視圖在我的應用程序,我用的是代表當細胞被竊聽得到通知,所以我實現didSelectItemAtIndexPath:方法。在斯威夫特2此方法的簽名是這樣的:CollectionViewDelegate方法沒有得到斯威夫特3遷移

@objc func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 

運行直通遷移過程後,這個簽名改成這樣:

@objc func collectionView(collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 

編譯器很高興,我提出的,但現在這種方法沒有按」當我點擊任何細胞時觸發。我找到了一種方法來解決它:

@objc(collectionView:didSelectItemAtIndexPath:) func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 

顯然,這個問題是在第一個參數的名字,但我還是不明白爲什麼它不沒有明確指定的OBJ-C選擇工作。

回答

2

不需要寫@objc,

這裏是一個工作的代碼,在viewDidLoad中

extension YourViewController:UICollectionViewDelegate{ 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     print("didSelect") 
    } 
} 
+0

我的實現是擴展了,但是協議一致性是視圖控制器聲明。編譯器給我的錯誤這樣的 「Objective-C的方法 '的CollectionView:didSelectItemAt:'):需求的選擇( ':didSelectItemAtIndexPath的CollectionView' 不匹配方法 'didSelectItemAt :)的CollectionView(_' 提供」。當我將協議符合性擴展到擴展時,所有工作都像魅力一樣。 Stil並不完全明白這裏出了什麼問題,我猜你需要詳細瞭解swift和obj-c的互操作性。 – wirrwarr

0

附加代表也加入uicollectionviewdelegae,uicollectionviewdatasource也在故事板,

這可以幫助我。