Ale*_*ehr 3 click ios uicollectionview swift
在我的应用程序中,我使用的是UICollectionView.现在我想在点击集合视图的任何单元格时开发一个UIAlertController.我开始使用以下代码:
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!")
}
}
Run Code Online (Sandbox Code Playgroud)
但不知何故,"点击了!" 永远不会打印.
小智 12
尝试下一个:
extension HomeViewController: UICollectionViewDataSource, UICollectionViewDelegate {
}
Run Code Online (Sandbox Code Playgroud)
要么
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
...
cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap(_:))))
}
func tap(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: self.collectionView)
let indexPath = self.collectionView.indexPathForItem(at: location)
if let index = indexPath {
print("Got clicked on index: \(index)!")
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6901 次 |
最近记录: |