Мне нужна помощь, чтобы передать данные из TableView в другой без использования глобального массива.
Когда я нажимаю кнопку, я хочу отправить продукт из UITableView (ProductsTableView) в другой UITableView (CartTableView), но проблема в том, что у меня нет перехода между этими двумя таблицами.
Как я могу отправить выбранный продукт из моего первого ВК, где у меня есть ProductsTableView, во второй ВК, где находится мой CartTableView?
Вот мой код:
// 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 с моим интерфейсом:
Спасибо!