首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发送者: UIImageView !== UIImageView

发送者: UIImageView !== UIImageView
EN

Stack Overflow用户
提问于 2019-09-06 10:14:06
回答 1查看 55关注 0票数 0

我正在尝试获得一个在CollectionViewCell中显示和图像的UIImagePicker。在假定发送者等于(==)到UIImageView的地方,函数代码被破坏。

我已经通过调试器多次修改代码,并尝试通过visibleCells函数调用单元,但都无济于事。

代码语言:javascript
复制
//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中显示选取器图像。我的调试器总是打印"code failed at if let“,并且在视图加载后,collectionView.reloadData从来没有显示过不同的图像。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-06 10:38:53

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

代码语言:javascript
复制
@objc func uploadPhoto(_ sender: UIImageView) {

至:

代码语言:javascript
复制
@objc func uploadPhoto(_ sender: UITapGestureRecognizer) {

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

代码语言:javascript
复制
@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)
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57814924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档