Swift 发件人:UIImageView!==CollectionViewCell中的UIImageView

Swift 发件人:UIImageView!==CollectionViewCell中的UIImageView,swift,uicollectionview,uicollectionviewcell,Swift,Uicollectionview,Uicollectionviewcell,我正在尝试让UIImagePicker在CollectionViewCell中显示和成像。假设发送方与UIImageView相等(=),则func代码被破坏 我已经通过调试器对代码进行了多次修改,并尝试通过visibleCells函数调用该单元格,但均无效 //上传函数的代码 @objc func上传照片(\发送方:UIImageView){ 让uploadPicker=UIImagePickerController() uploadPicker.delegate=self 对于self.col

我正在尝试让UIImagePicker在CollectionViewCell中显示和成像。假设发送方与UIImageView相等(=),则func代码被破坏

我已经通过调试器对代码进行了多次修改,并尝试通过visibleCells函数调用该单元格,但均无效

//上传函数的代码
@objc func上传照片(\发送方:UIImageView){
让uploadPicker=UIImagePickerController()
uploadPicker.delegate=self
对于self.collectionView.visibleCells中的单元格{
如果让uploadCell=单元格为?UploadImageCell{
打印(“Pickle-Rick和\(uploadCell.imageContainer)和\(sender)”)
如果uploadCell.imageContainer==发送方{
selectedCell=上传单元格
打印(“此代码有效,\(字符串(描述:selectedCell)))
}否则{
打印(“如果让,代码失败”)
}
}
}
当前(上载选择器,动画:true,完成:nil)
}
//CollectionView的代码
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
如果indexath.section==0{
让cell=collectionView.dequeueReusableCell(withReuseIdentifier:imageCellId,for:indexath)作为!UploadImageCell
//让imageOption=imageOption(rawValue:indexPath.row)
//cell.imageContainer.image=imageOption?.icon()
cell.imageContainer.AddGestureRecognitzer(UITapGestureRecognitzer(目标:self,操作:#选择器(上传照片(:)))
返回单元
}
/*其他部分使用不同的单元格*/
}
//CollectionViewCell的代码
类UploadImageCell:UICollectionViewCell{
//标记:-属性
弱var-ep:EditProfileController?
让imageContainer:UIImageView={
让imageContainer=UIImageView()
imageContainer.clipsToBounds=true
imageContainer.backgroundColor=.blue
imageContainer.isUserInteractionEnabled=true
imageContainer.TranslatesAutoResizezingMaskintoConstraints=false
返回图像容器
}()
让上传按钮:UIButton={
let button=UIButton()
setTitle(“上载图像”,用于:。正常)
button.backgroundColor=UIColor.red
button.setTitleColor(.black,表示:。正常)
button.layer.cornerRadius=5
button.layer.shadowColor=UIColor.darkGray.cgColor
button.layer.shadowOffset=CGSize(宽度:button.frame.width,高度:2)
button.layer.shadowRadius=5
button.layer.shadowOpacity=1.0
button.translatesAutoresizingMaskIntoConstraints=false
返回按钮
}()
重写初始化(帧:CGRect){
super.init(frame:frame)
背景颜色=白色
addSubview(图像容器)
NSLayoutConstraint.activate([
imageContainer.topAnchor.constraint(等式:topAnchor),
imageContainer.heightAnchor.constraint(等式:heightAnchor,乘数:0.9),
imageContainer.widthAnchor.constraint(等号:widthAnchor)
])
}
必需的初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}

预期结果是选择器图像将显示在CollectionViewCell.imageContainer中。我的调试器总是打印“如果让代码失败”,并且在任何时候都没有collectionView.reloadData在加载视图后显示不同的图像。

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

@objc func uploadPhoto(_ sender: UIImageView) {
致:

然后可以从手势的
视图
属性中获取图像视图

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

非常感谢你。我现在的问题是,如果我给imageContainer一个默认值,它在从选择器中选择一个图像后不会改变。您是否实现了UIImagePickerControllerDelegate方法?
@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)
}