gpt4 book ai didi

swift - 发件人 : UIImageView ! == CollectionViewCell 中的 UIImageView

转载 作者:行者123 更新时间:2023-11-28 13:28:59 24 4
gpt4 key购买 nike

我正在尝试让 UIImagePicker 在 CollectionViewCell 中显示和成像。 func 代码在 sender 应该等于 (==) 到 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 中。我的调试器总是打印“代码在 if let 时失败”,并且 collectionView.reloadData 在 View 加载后从未显示过不同的图像。

最佳答案

点击手势(或任何手势识别器)的选择器必须将实际的手势识别器作为该方法的唯一参数。所以你需要改变:

@objc func uploadPhoto(_ sender: UIImageView) {

到:

@objc func uploadPhoto(_ sender: UITapGestureRecognizer) {

然后您可以从手势的 view 属性中获取 ImageView 。

@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)
}

关于swift - 发件人 : UIImageView ! == CollectionViewCell 中的 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57814924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com