-4

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
    }
}
Evelyn
  • 186
  • 1
  • 4
  • 25
  • I don't see the code for your `CartCell`? Do you have `UIImageView` for that? That's assume you are already passing correct `Items` and it have `imageUrl` variable... – Tj3n Nov 07 '19 at 10:49
  • try replacing imageUrl data type from UIImage to UIImageView in HomeCell class – Saurabh Nov 07 '19 at 10:56
  • @Tj3n the CartCell just has nothing but IBOutlets for the labels and Image, which is why I didn't add it to my code, I can add it if you like if it helps, the data passes perfectly fine for the labels in the CartCell its just the Image that Im struggling with – Evelyn Nov 07 '19 at 10:56
  • Then it should just be `cell.itemImageView.sd_setImage(with: URL(string: items.imageUrl))`? Similar to your `HomeCell` code, not sure what's the problem – Tj3n Nov 07 '19 at 10:58
  • You already have a `configure(withItems:)` method in your `HomeCell` to display an item in a cell. Replicate the approach for your `CartCell` item.. – flanker Nov 07 '19 at 11:00
  • im getting this error once I press the Cartbtn in the Nav bar after I press the atcBtn (btn that passes the from the cell) ***Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value*** on that line of code you just posted – Evelyn Nov 07 '19 at 11:00
  • just updated the code to show you what im working with – Evelyn Nov 07 '19 at 11:15

2 Answers2

1

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
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
0

This appears to be where the problem is

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

and as you noted, that code doesn't really make sense... If you're storing a url (a string) in your cart object, then you can't cast that to an image with cell.imageUrl.image, right?

You would need to assign it to the url

cell.imageUrl = cart.imageUrl

Of course that will just pass the url to the cell. The cell would then need some intelligence to get that associated image from the url.

Some pseudo code for your CartCell class...

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

cell.setImageUrlAndDisplayImage( cart.imageUrl )

and then the function in the CartCell class

func setImageUrlAndDisplayImage( imageUrl: URL) {
    self.setImage(with: URL(string: imageUrl))
}

or of course, you could just assign the image directly to the CartCell image property if it has one.

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

cell.the_image = UIImage(with: URL(string: cart.imageUrl))

The above is just pseudo code since we don't know what you Cart class or CartCell class looks like.

Jay
  • 34,438
  • 18
  • 52
  • 81