简体   繁体   中英

How to pass user image from Tablecell to Viewcontroller on firebase?

I was only able to pass user name labels from table cell to another Viewcontroller but not the user image. I tried using prepare for segue function as you can see in my code below. thanks

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage

class MainVc: UIViewController, UITableViewDelegate, UITableViewDataSource {




@IBOutlet weak var tableview2: UITableView!
var user = [User]()

override func viewDidLoad() {
    super.viewDidLoad()



    retrieveUsers()


}

func retrieveUsers() {
    let ref = Database.database().reference()

    ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

        let users = snapshot.value as! [String : AnyObject]
        self.user.removeAll()
        for (_, value) in users {
            if let uid = value["uid"] as? String {
                if uid != Auth.auth().currentUser!.uid {
                    let userToShow = User()
                    if let fullName = value["full name"] as? String,let businessType = value["Business Type"] as? String, let imagePath = value["urlToImage"] as? String {

                        userToShow.fullName = fullName
                        userToShow.businessType = businessType

                        userToShow.imagePath = imagePath
                        userToShow.userID = uid

                        self.user.append(userToShow)


                    }

                }



            }



        }


        self.tableview2.reloadData()
    })

    ref.removeAllObservers()

}




 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {



    let upcoming: ViewController = segue.destination as! ViewController

    let indexPath = self.tableview2.indexPathForSelectedRow!

    let titleString = self.user[indexPath.row].fullName


    let imageView = self.user[indexPath.row].imagePath! // i tried using this to transfer the image  from tableview to viewcontroller but its not working 

    upcoming.imageView = imageView

    upcoming.titleString = titleString


    self.tableview2.deselectRow(at: indexPath, animated: true)



}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return user.count ?? 0
}



func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview2.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! UserCell


    cell.userNameLbl.text = self.user[indexPath.row].fullName
     cell.businessTypeLbl.text = self.user[indexPath.row].businessType

    cell.userID = self.user[indexPath.row].userID
    cell.userImage.downloadImage(from: self.user[indexPath.row].imagePath!)

    return cell
}    
}   

extension UIImageView {

    func downloadImage(from imgURL: String!) {
        let url = URLRequest(url: URL(string: imgURL)!)

        let task = URLSession.shared.dataTask(with: url) {
            (data, response, error) in

            if error != nil {
                print(error!)
                return
            }

            DispatchQueue.main.async {
                self.image = UIImage(data: data!)
            }

        }

        task.resume()
    }


    }

class ViewController: UIViewController {

@IBOutlet weak var profileImage: UIImageView!

@IBOutlet weak var testLbl: UILabel!

var titleString: String!
var imageView: String!



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

     self.testLbl.text = self.titleString
     self.profileImage.image = UIImage(named: imageView!)



}

import UIKit

class User: NSObject {

var userID: String!
var fullName: String!
var imagePath: String!
var businessType: String!

}

You can define UIImage or image name property in your ViewController and access it from your MainVc . I have passed image name below.

class ViewController: UIViewController {

     @IBOutlet weak var profileImage: UIImageView!

     @IBOutlet weak var testLbl: UILabel!

     var titleString: String!
     var yourImageName:String!
     override func viewDidLoad() {
     super.viewDidLoad()

     self.testLbl.text = self.titleString
     self.imageview.image = UIImage(named:yourImageName)
}

and pass image Name in your prepare for segue as

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let upcoming: ViewController = segue.destination as! ViewController
    let indexPath = self.tableview.indexPathForSelectedRow!
    let titleString = self.user[indexPath.row].fullName
    let imageName = self.user[indexPath.row].imagename // access image name from user array
    upcoming.titleString = titleString
    upcoming.yourImageName = imageName

    // here i want to pass user image as welH from cell to the viewcontroller
    self.tableview.deselectRow(at: indexPath, animated: true)
 }

Hope it helps. Happy Coding!!

self.profileImage.self.downloadImage(来自:imageString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM