选择器获取 indexPath UICollectionView Swift 3.0
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)
您不能传递 selector
或 UIGestureRecognizer
的其他实例,您唯一可以传递的实例是 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)
}
}
编辑: 如果您想 show/hide 在单元格上双击图像,那么您需要使用单元格的 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
}
现在在 UITapGestureRecognizer
的 doubleTapped
操作上设置 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
}
}
}
需要这样添加Selector
let tap = UITapGestureRecognizer(target: self, action: #selector(YourViewControllerName.doubleTapped(_:)))
subOptioncell.addGestureRecognizer(tap)
您的情况下正确的选择器是 doubleTapped:
。即
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped:))
调用目标方法时不能触发任意参数。您可以通过
在 subOptioncell
上设置目标
let tap = UITapGestureRecognizer(target: subOptioncell, action: #selector(doubleTapped:))
你可以在 subOptioncell
中任意设置 object.parameter
将您的代码更改为。
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)")
}
编码愉快!!!
我试图在单元格被点击两次时得到 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)
您不能传递 selector
或 UIGestureRecognizer
的其他实例,您唯一可以传递的实例是 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)
}
}
编辑: 如果您想 show/hide 在单元格上双击图像,那么您需要使用单元格的 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
}
现在在 UITapGestureRecognizer
的 doubleTapped
操作上设置 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
}
}
}
需要这样添加Selector
let tap = UITapGestureRecognizer(target: self, action: #selector(YourViewControllerName.doubleTapped(_:)))
subOptioncell.addGestureRecognizer(tap)
您的情况下正确的选择器是 doubleTapped:
。即
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped:))
调用目标方法时不能触发任意参数。您可以通过
在subOptioncell
上设置目标
let tap = UITapGestureRecognizer(target: subOptioncell, action: #selector(doubleTapped:))
你可以在 subOptioncell
将您的代码更改为。
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)")
}
编码愉快!!!