ios - 将对象从 UITableView 发送到另一个没有全局数组的 UITableView
问题描述
我需要一些帮助才能将数据从 TableView 传递到另一个而不使用全局数组。当我按下按钮时,我想将产品从 UITableView (ProductsTableView) 发送到另一个 UITableView (CartTableView),但问题是我在这两个表之间没有 segue。如何将所选产品从我拥有 ProductsTableView 的第一个 VC 发送到我的 CartTableView 所在的第二个 VC?
这是我的代码:
// First VC
class ProductsViewController: UIViewController{
@IBOutlet weak var productsTableView: UITableView!
// Function to add the product into the cart
@IBAction func addProductToCartButton(_ sender: UIButton) {
// Start animation region
let buttonPosition : CGPoint = sender.convert(sender.bounds.origin, to: self.productsTableView)
let indexPath = self.productsTableView.indexPathForRow(at: buttonPosition)!
let cell = productsTableView.cellForRow(at: indexPath) as! ProductTableViewCell
let imageViewPosition : CGPoint = cell.productImageView.convert(cell.productImageView.bounds.origin, to: self.view)
let imgViewTemp = UIImageView(frame: CGRect(x: imageViewPosition.x, y: imageViewPosition.y, width: cell.productImageView.frame.size.width, height: cell.productImageView.frame.size.height))
imgViewTemp.image = cell.productImageView.image
animationProduct(tempView: imgViewTemp)
// End animation region
// Start appending product to cart
// TODO: add the logic for append here
// End appending product to cart
showAlertWith(title: "Added", message: "Product added with success.")
}
// "Product" is my entity from Core Data
func appendProductToCartList(product: Product){
}
// Function to send details about a product from this VC to another VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let selectedIndexPath = productsTableView.indexPathForSelectedRow {
let cartVC: CartViewController = segue.destination as! CartViewController
cartVC.cartProductsArray = [productsArray[selectedIndexPath.row]]
}
}
}
// Second VC
class CartViewController: UIViewController {
@IBOutlet weak var cartTableView: UITableView!
var cartProductsArray = [Product]()
}
这是我的界面的 GIF:
谢谢 !
解决方案
推荐阅读
- python - 为什么我在 python 2.7.14 中出现语法错误
- java - 为什么 JDBC 是动态加载而不是导入的?
- webpack - 无法使用模块将 Three.js 导入 Vue
- vuejs2 - Vue 2.5 中的文字对象和属性反应性
- json - JSON api 适用于本地,但不适用于公共部署
- r - R - (Tidyverse)在间隔内向数据框添加行
- qt - QtQuick lower() 和 raise() 在 close() 正常工作时不起作用
- google-maps-api-3 - 在 Google 地图中添加本地化图钉
- r - 根据 dplyr 中的两个条件过滤行
- java - 计数器方法不递增