有没有办法从一个班级到另一个班级访问插座?

时间:2019-06-03 01:08:55

标签: ios swift firebase-realtime-database

我试图从集合视图单元格到主视图控制器获取UIImageView。我尝试了很多,但找不到解决方案。 这是我的代码: (即尝试访问从uploadsubpostcell到VC类内部的if语句的PreviewStep。if语句位于picker函数下。)

轻松修复,只需执行cell.(name).image!

class UploadSubPostCell: UICollectionViewCell {

    @IBOutlet weak var previewStep: UIImageView!

}

class UploadViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var previewImage: UIImageView!
    @IBOutlet weak var postBtn: UIButton!
    @IBOutlet weak var selectBtn: UIButton!

    @IBOutlet weak var selectStepsBtn: UIButton!


    var picker = UIImagePickerController()
    var isThumbnailImage = true
    var subpostsArray = [UIImage]()
    var subposts = [SubPost]()
    var posts = [Post]()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavigationBarItems()
        picker.delegate = self

    }

    func setupNavigationBarItems() {
        navigationItem.title = "Upload"
    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerEditedImage] as? UIImage {

            if isThumbnailImage {
                previewImage.image = image
            } else {
                previewStep.image = image
            }

            selectBtn.isHidden = false
            selectStepsBtn.isHidden = false
            postBtn.isHidden = false
            if isThumbnailImage == false {
                subpostsArray.append(image)
                subposts.count + 1
                print("Appended image to array:", subpostsArray)
            }
        }

        picker.dismiss(animated: true, completion: nil)
    }


    @IBAction func selectStepPressed(_ sender: Any) {

        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        isThumbnailImage = false

        self.present(picker, animated: true, completion: nil)

    }


    @IBAction func selectPressed(_ sender: Any) {

        picker.allowsEditing = true
        picker.sourceType = .photoLibrary
        isThumbnailImage = true

        self.present(picker, animated: true, completion: nil)

    }

    @IBAction func addNewPressed(_ sender: Any) {
    }


    @IBAction func postPressed(_ sender: Any) {
        AppDelegate.instance().showActivityIndicator()

        let uid = Auth.auth().currentUser!.uid
        let ref = Database.database().reference()
        let storage = Storage.storage().reference(forURL: "gs://mobile-d9fcd.appspot.com")

        let key = ref.child("posts").childByAutoId().key
        let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
        let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)

        let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
            if error != nil {
                print(error!.localizedDescription)
                AppDelegate.instance().dismissActivityIndicator()
                return
            }
                imageRef.downloadURL(completion: { (url, error) in
                    if let url = url {
                        let feed = ["userID" : uid,
                                    "pathToImage" : url.absoluteString,
                                    "likes" : 0,
                                    "author" : Auth.auth().currentUser!.displayName!,
                                    "postID" : key] as [String : Any]

                        let postFeed = ["\(key)" : feed]

                        ref.child("posts").updateChildValues(postFeed)
                        AppDelegate.instance().dismissActivityIndicator()
                        self.picker.dismiss(animated: true, completion: nil)
                        }
                    })
                }
            uploadTask.resume()
        }
        func numberOfSections(in collectionView: UICollectionView) -> Int {
            return 1

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return self.subposts.count
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell

            cell.postImage.downloadImage(from: self.posts[indexPath.row].pathToImage)
            cell.authorLabel.text = self.posts[indexPath.row].author
            cell.likeLabel.text = "\(self.posts[indexPath.row].likes!) Likes"
            cell.postID = self.posts[indexPath.row].postID

        }
    }
}

1 个答案:

答案 0 :(得分:2)

@Matt是正确的,在您的情况下,您应该更新由collectionView呈现的数据模型,并调用reloadDatareloadItems(at:)

    self.updateModel();
    self.collectioView.reloadData()
    self.collectioView.reloadItems(at: [IndexPath(item: 0, section: 0)]);

但是如果您需要直接访问visible cells的权限,则可以使用其他工作流程:

    let image : UIImage? = nil
    let path = IndexPath(item: 0, section: 0)
    let visiblePaths = self.collectioView.indexPathsForVisibleItems
    if visiblePaths.contains(path) {
        if let cell = self.collectioView.cellForItem(at: path) as? UploadSubPostCell {
            cell.previewStep.image = image;
        }
    }

您可以使用这种工作流程仅更新可见的单元格,因为不可见的单元格将在显示到屏幕之前被重用