[英]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我如何能够将图像从 HomeViewController 显示到 CartViewController
I have the code setup in my cells to where the data passes from one VC to another,我在我的单元格中设置了代码,以便数据从一个 VC 传递到另一个,
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按下 atcBtn 后,当数据从 HomeVC 传递到 CartVC 时,我将如何显示图像
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我从堆栈中尝试了几种方法,但在 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
}
}
The code you posted doesn't quite match up:您发布的代码不太匹配:
In cellForRowAt
in CartViewController
, for example, you are using CartCell
but your code is setting:例如,在CartViewController
的cellForRowAt
中,您正在使用CartCell
但您的代码正在设置:
cell.store.text = cart.items.store
but there is no store
label / property in your posted CartCell
.但是您发布的CartCell
中没有store
label / 属性。
However, since you are doing very similar things with HomeCell
class, just take the same approach for CartCell
.但是,由于您使用HomeCell
class 执行非常相似的操作,因此只需对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
}
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 cell.imageUrl.image = cart.imageUrl // 无法弄清楚如何让这个代码工作,因为它是一个图像到字符串的问题
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?正如您所指出的,该代码实际上没有意义...如果您在购物车 object 中存储 url(一个字符串),那么您不能将其转换为带有 cell.imageUrl.image 的图像,对?
You would need to assign it to the url您需要将其分配给 url
cell.imageUrl = cart.imageUrl
Of course that will just pass the url to the cell.当然,这只会将 url 传递给单元。 The cell would then need some intelligence to get that associated image from the url.然后,该单元将需要一些智能来从 url 获取相关图像。
Some pseudo code for your CartCell class... 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然后是 CartCell class 中的 function
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.或者,当然,您可以直接将图像分配给 CartCell 图像属性(如果有的话)。
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.以上只是伪代码,因为我们不知道您的 Cart class 或 CartCell class 是什么样的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.