我在轻按卡片以进行翻转时遇到问题。在我测试打印功能时,没有调用did select函数,这表明它根本没有轻按。可以帮忙吗?
'''
@ IBOutlet弱var collectionView:UICollectionView!
var model = CardModel()
//Kepp track of the cards veiwed
var cardArray = [Card]()
override func viewDidLoad() {
super.viewDidLoad()
//Call the getCards method of the card model
cardArray = model.getCards()
collectionView.delegate = self
collectionView.dataSource = self
}
// MARK: -UICOlecctionView Protocol Methods
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cardArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Get a card CardCollectionViewCell object
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! CardCollectionViewCell
//Get the card that the collection view is trying to display
let card = cardArray [indexPath.row]
//Set the card for the cell
cell.setCard(card)
return cell
}
func collectionView( _ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
print("Cell is taped: \(indexPath.row)")
let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell
// Flip the card
cell.flip()
}
}
'''
我同意@toto。您的大多数代码看起来不错,但我认为您可能会遗漏一些东西。这是我自己的“收藏夹视图”功能的示例:
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return userDetails.count
}
[似乎您可能只是想念return
-I have an extensive Collection View(由Firebase FYI所吸引),对您也有所帮助!祝你好运!