[英]Selector to get indexPath UICollectionView Swift 3.0
我試圖在兩次點擊時在單元格上獲取indexPath
。 我像這樣在Selector
傳遞參數,但它給出了錯誤。 正確的格式是什么?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let subOptioncell : SubOptionsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: subOptionsCVReuseIdentifier, for: indexPath) as! SubOptionsCollectionViewCell
let imageNamed = "\(customizeOptionSelected[indexPath.row])"
subOptioncell.subOptionsImage.image = UIImage(named: imageNamed)
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped(sender: indexPath)))
tap.numberOfTapsRequired = 2
collectionView.addGestureRecognizer(tap)
return subOptioncell
}
}
func doubleTapped(sender: IndexPath) {
print("Double Tap")
}
首先,您要將tapGesture添加到collectionView
而不是subOptioncell
。
subOptioncell.addGestureRecognizer(tap)
collectionView.addGestureRecognizer(tap)
您不能使用UIGestureRecognizer
selector
傳遞其他實例,唯一可以傳遞的實例是UI(Tap)GestureRecognizer
。 如果您想要該單元格的indexPath,可以嘗試這樣。 首先,像這樣設置TapGesture
的selector
。
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped(sender:)))
現在方法應該是這樣的:
func doubleTapped(sender: UITapGestureRecognizer) {
if let cell = sender.view as? SubOptionsCollectionViewCell, let indexPath = self.collectionView.indexPath(for: cell) {
print(indexPath)
}
}
編輯:如果要在單元格上雙擊顯示/隱藏圖像,則需要使用indexPath
處理該indexPath
,為此首先聲明一個IndexPath
實例,然后在cellForItemAt indexPath
使用它。
var selectedIndexPaths = IndexPath()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//Your code
//Now add below code to handle show/hide image
cell.subOptionSelected.isHidden = self.selectedIndexPaths != indexPath
return cell
}
現在doubleTapped
的行動UITapGestureRecognizer
設置selectedIndexPath
。
func doubleTapped(sender: UITapGestureRecognizer) {
if let cell = sender.view as? SubOptionsCollectionViewCell, let indexPath = self.collectionView.indexPath(for: cell) {
if self.selectedIndexPaths == indexPath {
cell.subOptionSelected.isHidden = true
self.selectedIndexPaths = IndexPath()
}
else {
cell.subOptionSelected.isHidden = false
self.selectedIndexPaths = indexPath
}
}
}
您所用的正確選擇器是doubleTapped:
那是
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped:))
調用目標方法時,不能激發任意參數。 您可以通過以下方式在subOptioncell
上設置目標
let tap = UITapGestureRecognizer(target: subOptioncell, action: #selector(doubleTapped:))
您可以在subOptioncell
設置所需的任意任意subOptioncell
您需要以這種方式添加選擇器
let tap = UITapGestureRecognizer(target: self, action: #selector(YourViewControllerName.doubleTapped(_:)))
subOptioncell.addGestureRecognizer(tap)
將您的代碼更改為。
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped(_:)))
和功能。
func doubleTapped(_ sender: AnyObject) {
print("Double Tap")
}
在您的數據源方法
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let subOptioncell : SubOptionsCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: subOptionsCVReuseIdentifier, for: indexPath) as! SubOptionsCollectionViewCell{
//... your code
subOptioncell.addGestureRecognizer(tap)
return subOptioncell
}
}
然后在函數cellTapped()
func cellTapped(sender: UITapGestureRecognizer){
let tapLocation = sender.location(in: yourCollectionView)
let indexPath : IndexPath = yourCollectionView.indexPathForItem(at: tapLocation)!
var currentIndex = 0
if let cell = yourCollectionView.cellForItem(at: indexPath){
currentIndex = cell.tag
}
print("Your Selected Index : \(currentIndex)")
}
快樂編碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.