Search code examples
iosswiftfirebaseuitableviewsdwebimage

How would I be able to pass an image when data is passed from one View Controller to another?


How would I be able to show an image from HomeViewController to the CartViewController

I have the code setup in my cells to where the data passes from one VC to another,

Im trying to present the image when the data is passed

How would I be able to show the image when data is passed from the HomeVC to the CartVC after the atcBtn is pressed

all the data in my labels passes fine its just the image data that fails to pass

I have tried a few ways from stack but I still keep getting error codes on presenting the image in the CartVC

class CartViewController: UIViewController {

    var items: Items!
    @IBOutlet weak var cartTableView: UITableView!

     override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        cartTableView.dataSource = self
        cartTableView.delegate = self
    }

}

extension CartViewController: UITableViewDataSource, UITableViewDelegate {

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Cart.currentCart.cartItems.count
    }

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

        let cart = Cart.currentCart.CartItems[indexPath.row]

        cell.store.text = cart.items.store
        cell.lblMealName.text = (cart.items.name)
        cell.lblSubTotal.text = "$\(cart.items.cost)"

        cell.imageUrl.image = cart.imageUrl   // can't figure out how to get this to code to work since it is an Image to String issue

        return cell

class CartCell: UITableViewCell {

    @IBOutlet weak var lblMealName: UILabel!
    @IBOutlet weak var imageUrl: UIImageView!
    @IBOutlet weak var lblSubTotal: UILabel!
    @IBOutlet weak var lblWeight: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}

Solution

  • The code you posted doesn't quite match up:

    In cellForRowAt in CartViewController, for example, you are using CartCell but your code is setting:

    cell.store.text = cart.items.store
    

    but there is no store label / property in your posted CartCell.

    However, since you are doing very similar things with HomeCell class, just take the same approach for CartCell.

    Something along these lines:

    class CartCell: UITableViewCell {
    
        @IBOutlet weak var lblMealName: UILabel!
        @IBOutlet weak var imageUrl: UIImageView!
        @IBOutlet weak var lblSubTotal: UILabel!
        @IBOutlet weak var lblWeight: UILabel!
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        func configure(withItems items: Items) {
            //store.text = cart.items.store
            lblMealName.text = (items.name)
            lblSubTotal.text = "$\(items.cost)"
            imageUrl.sd_setImage(with: URL(string: items.imageUrl))
        }
    
    }
    

    and change `cell

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath) as! CartCell
    
        let cart = Cart.currentCart.CartItems[indexPath.row]
    
        //cell.store.text = cart.items.store
        //cell.lblMealName.text = (cart.items.name)
        //cell.lblSubTotal.text = "$\(cart.items.cost)"
    
        //cell.imageUrl.image = cart.imageUrl   // can't figure out how to get this to code to work since it is an Image to String issue
    
        cell.configure(withItem: cart)
    
        return cell
    }