UITapGestureRecognizer 不改变 CollectionViewCell 的内容。 Swift
UITapGestureRecognizer not changing contents of CollectionViewCell . Swift
我有一个包含许多不同图片的集合视图,我希望当点击图片时,它周围会出现一个边框。
我制作了一个自定义单元格和一个 UICollectionViewCell,其中 imageView 嵌入到图片的另一个视图(即轮廓)中。所以我设置了一个 UITapGestureRecognizer 来获取索引,但是当我将外部视图设置为有一个边框时它不起作用。
这是我的手机:
import UIKit
class PicViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var imageOutline: UIView!
}
这是在我的 UICollectionViewController 中:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PicViewCell
// Configure the cell
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
cell.imageOutline.addGestureRecognizer(tapGesture)
return cell
点击手势的方法如下
func handleTap(sender: UITapGestureRecognizer) {
print("tap")
if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {
let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PicViewCell
cell.imageOutline.layer.borderWidth = 5
} else {
print("")
}
}
在您的 handleTap
例程中,您调用了错误的方法来检索单元格。你想要 cellForItem(at:)
它将检索现有的单元格或 return nil
如果它不在屏幕上:
let cell = collectionView?.cellForItem(at: indexPath) as! PicViewCell
备选方案
如果您正在修改您正在点击的视图,您可以作为 sender
的 view
访问它:
func handleTap(sender: UITapGestureRecognizer) {
if let imageOutline = sender.view as? UIImageView {
imageOutline.layer.borderWidth = 5
}
}
注意:在这两种解决方案中,您应该在勾勒出单元格时更新您的模型,这样如果该单元格滚出屏幕然后又回到屏幕上,您可以使用该模型数据在 collectionView(_:cellForItemAt:)
.
的覆盖中正确设置单元格的轮廓
我有一个包含许多不同图片的集合视图,我希望当点击图片时,它周围会出现一个边框。
我制作了一个自定义单元格和一个 UICollectionViewCell,其中 imageView 嵌入到图片的另一个视图(即轮廓)中。所以我设置了一个 UITapGestureRecognizer 来获取索引,但是当我将外部视图设置为有一个边框时它不起作用。
这是我的手机:
import UIKit
class PicViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var imageOutline: UIView!
}
这是在我的 UICollectionViewController 中:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PicViewCell
// Configure the cell
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
cell.imageOutline.addGestureRecognizer(tapGesture)
return cell
点击手势的方法如下
func handleTap(sender: UITapGestureRecognizer) {
print("tap")
if let indexPath = self.collectionView?.indexPathForItem(at: sender.location(in: self.collectionView)) {
let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PicViewCell
cell.imageOutline.layer.borderWidth = 5
} else {
print("")
}
}
在您的 handleTap
例程中,您调用了错误的方法来检索单元格。你想要 cellForItem(at:)
它将检索现有的单元格或 return nil
如果它不在屏幕上:
let cell = collectionView?.cellForItem(at: indexPath) as! PicViewCell
备选方案
如果您正在修改您正在点击的视图,您可以作为 sender
的 view
访问它:
func handleTap(sender: UITapGestureRecognizer) {
if let imageOutline = sender.view as? UIImageView {
imageOutline.layer.borderWidth = 5
}
}
注意:在这两种解决方案中,您应该在勾勒出单元格时更新您的模型,这样如果该单元格滚出屏幕然后又回到屏幕上,您可以使用该模型数据在 collectionView(_:cellForItemAt:)
.