swift - 发件人: UIImageView !== CollectionViewCell 中的 UIImageView
问题描述
我试图让 UIImagePicker 在 CollectionViewCell 中显示和图像。func 代码在发件人被认为与 UIImageView 相等 (==) 的地方被破坏。
我已经通过调试器多次修改了代码,并试图通过 visibleCells 函数调用单元格,但无济于事。
//Code for upload func
@objc func uploadPhoto(_ sender: UIImageView) {
let uploadPicker = UIImagePickerController()
uploadPicker.delegate = self
for cell in self.collectionView.visibleCells {
if let uploadCell = cell as? UploadImageCell {
print("Pickle Rick and \(uploadCell.imageContainer) and \(sender)")
if uploadCell.imageContainer == sender {
selectedCell = uploadCell
print("This code works, \(String(describing: selectedCell))")
} else {
print("code failed at if let")
}
}
}
present(uploadPicker, animated: true, completion: nil)
}
// Code for CollectionView
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: imageCellId, for: indexPath) as! UploadImageCell
//let imageOption = ImageOption(rawValue: indexPath.row)
//cell.imageContainer.image = imageOption?.icon()
cell.imageContainer.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(uploadPhoto(_:))))
return cell
}
/* other sections use different cells*/
}
//code for CollectionViewCell
class UploadImageCell: UICollectionViewCell {
// Mark: - Properties
weak var ep: EditProfileController?
let imageContainer: UIImageView = {
let imageContainer = UIImageView()
imageContainer.clipsToBounds = true
imageContainer.backgroundColor = .blue
imageContainer.isUserInteractionEnabled = true
imageContainer.translatesAutoresizingMaskIntoConstraints = false
return imageContainer
}()
let uploadButton: UIButton = {
let button = UIButton()
button.setTitle("Upload Image", for: .normal)
button.backgroundColor = UIColor.red
button.setTitleColor(.black, for: .normal)
button.layer.cornerRadius = 5
button.layer.shadowColor = UIColor.darkGray.cgColor
button.layer.shadowOffset = CGSize(width: button.frame.width, height: 2)
button.layer.shadowRadius = 5
button.layer.shadowOpacity = 1.0
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
addSubview(imageContainer)
NSLayoutConstraint.activate([
imageContainer.topAnchor.constraint(equalTo: topAnchor),
imageContainer.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 0.9),
imageContainer.widthAnchor.constraint(equalTo: widthAnchor)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
预期结果是选择器图像显示在 CollectionViewCell.imageContainer 中。我的调试器总是打印“如果让代码失败”,并且在加载视图后,collectionView.reloadData 绝不会显示不同的图像。
解决方案
点击手势(或任何手势识别器)的选择器必须将实际手势识别器作为方法的唯一参数。所以你需要改变:
@objc func uploadPhoto(_ sender: UIImageView) {
至:
@objc func uploadPhoto(_ sender: UITapGestureRecognizer) {
然后您可以从手势的view
属性中获取图像视图。
@objc func uploadPhoto(_ sender: UITapGestureRecognizer) {
guard let imageView = sender.view as? UIImageView { else return }
let uploadPicker = UIImagePickerController()
uploadPicker.delegate = self
for cell in self.collectionView.visibleCells {
if let uploadCell = cell as? UploadImageCell {
print("Pickle Rick and \(uploadCell.imageContainer) and \(sender)")
if uploadCell.imageContainer == imageView {
selectedCell = uploadCell
print("This code works, \(String(describing: selectedCell))")
} else {
print("code failed at if let")
}
}
}
present(uploadPicker, animated: true, completion: nil)
}
推荐阅读
- visual-studio - Visual Studio 通过 ssh 在 ubuntu 上调试 .net 核心应用程序
- java - Android 应用程序开发支持哪些 Java 版本。我可以使用超过 1.8 或 java 8 的 java 版本吗
- unity3d - 如何在 Unity 中实现滑动?
- python - 如何恢复已删除的 C:\Program Files\Sublime Text 3\Packages\Python.sublime-package?
- django-models - 如何在 django 中使用表单更新/编辑数据库内容
- informatica - 最好执行左连接的连接器或查找
- c++ - 使用 PCL 调整粒子滤波的参数
- javascript - 如何使用javascript为列表中的每个元素添加删除按钮
- c# - C# Entityframework 核心如何流畅地映射配置多个实体之间的公共属性
- python - 检查变量python是否存在