我试图在细胞被点击两次时获得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")
}
答案 0 :(得分:2)
首先,您要将tapGesture添加到collectionView
而不是subOptioncell
。
subOptioncell.addGestureRecognizer(tap)
collectionView.addGestureRecognizer(tap)
您无法通过selector
的{{1}}传递其他实例,您可以传递的唯一实例是UIGestureRecognizer
。如果你想要那个单元格的indexPath,你可以尝试这样。首先设置UI(Tap)GestureRecognizer
selector
这样的TapGesture
。
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
单元格来处理它,因为它首先声明一个{{1}的实例并在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
}
的{{1}}动作设置了doubleTapped
。
UITapGestureRecognizer
答案 1 :(得分:1)
您案例中的正确选择器为doubleTapped:
。那是
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped:))
调用目标方法时,不能触发任意参数。您可以通过
在subOptioncell
上设置目标
let tap = UITapGestureRecognizer(target: subOptioncell, action: #selector(doubleTapped:))
你可以在subOptioncell
答案 2 :(得分:0)
您需要像这样添加选择器
@Override
public void onRadioStarted() {
mTextViewControl = (TextView) findViewById(R.id.textviewControl);
mTextViewControl.post(new Runnable() {
@Override
public void run() {
mTextViewControl.setText("RADIO STATE : STOPPED.");
}
});
}
答案 3 :(得分:0)
将您的代码更改为。
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped(_:)))
和函数。
func doubleTapped(_ sender: AnyObject) {
print("Double Tap")
}
答案 4 :(得分:0)
在您的数据源方法
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)")
}
快乐编码!!!