在我的應用程序中,我使用的是UICollectionView。現在我想開發一個UIAlertController,點擊集合視圖中的任何單元格。 我開始用下面的代碼: 「GOT點擊」Swift:點擊UICollectionView的單元格並打開AlertViewController
extension HomeViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
…
}
// specify cells
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
….
}
// called when widget is moved
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
…
}
// called when clicked
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Got clicked!")
}
}
但不知何故,從不打印。
你設置委託和數據源? – ronatory
不,我該怎麼做?對不起,我是初學者:D –
@ AlexanderJeitler-Stehr,知道你已經開始iOS和學習Swift,這真是太好了。你只是錯過添加** UICollectionViewDelegate **到擴展。只需在** UICollectionViewDataSource **之後添加它,您就可以輕鬆前往。確保你已經將委託綁定到'HomeViewController'。快樂編碼:) –