我将如何使用闭包传递数据?
How would I pass data using a closure?
我正在尝试使用我的 HomeCell 中的闭包 (addActionHandler) 将数据从我的 HomeViewController 中的单元格传递到我的 CartViewController。
我之前的工作是在 HomeVC 的 cellForRowAt 中使用 Tray.currentCart.cartItems.append(item)
传递数据。
但是由于我通过删除 static let currentCart = Tray()
修改了我的 托盘 class,我似乎无法让 HomeVC 中的单元格将数据传递给 CartVC。
在 Home Cell 中按下 ATCBtn 后,我如何才能修改代码以使其再次工作以将数据从 HomeVc 传递到 CartVC
我非常接近我的解决方案我只是不知道从这里到哪里才能让它工作...任何帮助将不胜感激
import UIKit
import Firebase
import FirebaseFirestore
class HomeViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var cartBtn: UIBarButtonItem! //segues data to CartVC
var tray: [Tray] = []
var itemSetup: [Items] = []
var selectedItem: Items?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
tableView.delegate = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CartViewController {
vc.items = self.selectedItem
}
}
}
extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemSetup.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell") as? TestCell else { return UITableViewCell() }
let item = itemSetup[indexPath.row]
cell.configure(withItems: item)
cell.addActionHandler = { (option: Int) in
print("Option selected = \(option)")
// Tray.currentCart.cartItems.append(item) //old code that passed data for selected cell in HomeVC to cartVC
item.selectedOption = option
}
return cell
}
}
class CartViewController: UIViewController {
var items: Items!
var tray: [Tray] = []
@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 tray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath) as! CartCell
// let cart = Tray.currentCart.cartItems[indexPath.row] // code that was used to populate data in CartCells switched with new code below
let cart = tray[indexPath.row].cartItems[indexPath.section]
cell.configure(withItems: cart)
return cell
}
}
class Tray {
var cartItems = [Items]()
var cart: Items!
var brandName: String!
// static let currentCart = Tray() code that has been removed
}
设置class托盘如下
class Tray {
var cart: Items
var brandName: String
init(cart: Items,
brandName: String) {
self.cart = cart
self.brandName = brandName
}
}
在 HomeViewController 中,在 addActionHandler 下将所选项目附加到购物车
cell.addActionHandler = { (option: Int) in
print("Option selected = \(option)")
item.selectedOption = option
tray.append(Tray(cart: item, brandName: "<Brand Name>"))
}
将托盘[]传递给 CartViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CartViewController {
vc.items = self.selectedItem
vc.tray = self.tray
}
}
在 CartViewController 中,将项目传递给 cartCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath) as! CartCell
let cart = tray[indexPath.row]
cell.configure(withItems: cart.cart)
return cell
}
我正在尝试使用我的 HomeCell 中的闭包 (addActionHandler) 将数据从我的 HomeViewController 中的单元格传递到我的 CartViewController。
我之前的工作是在 HomeVC 的 cellForRowAt 中使用 Tray.currentCart.cartItems.append(item)
传递数据。
但是由于我通过删除 static let currentCart = Tray()
修改了我的 托盘 class,我似乎无法让 HomeVC 中的单元格将数据传递给 CartVC。
在 Home Cell 中按下 ATCBtn 后,我如何才能修改代码以使其再次工作以将数据从 HomeVc 传递到 CartVC
我非常接近我的解决方案我只是不知道从这里到哪里才能让它工作...任何帮助将不胜感激
import UIKit
import Firebase
import FirebaseFirestore
class HomeViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var cartBtn: UIBarButtonItem! //segues data to CartVC
var tray: [Tray] = []
var itemSetup: [Items] = []
var selectedItem: Items?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableView.dataSource = self
tableView.delegate = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CartViewController {
vc.items = self.selectedItem
}
}
}
extension HomeViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemSetup.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TestCell") as? TestCell else { return UITableViewCell() }
let item = itemSetup[indexPath.row]
cell.configure(withItems: item)
cell.addActionHandler = { (option: Int) in
print("Option selected = \(option)")
// Tray.currentCart.cartItems.append(item) //old code that passed data for selected cell in HomeVC to cartVC
item.selectedOption = option
}
return cell
}
}
class CartViewController: UIViewController {
var items: Items!
var tray: [Tray] = []
@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 tray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath) as! CartCell
// let cart = Tray.currentCart.cartItems[indexPath.row] // code that was used to populate data in CartCells switched with new code below
let cart = tray[indexPath.row].cartItems[indexPath.section]
cell.configure(withItems: cart)
return cell
}
}
class Tray {
var cartItems = [Items]()
var cart: Items!
var brandName: String!
// static let currentCart = Tray() code that has been removed
}
设置class托盘如下
class Tray {
var cart: Items
var brandName: String
init(cart: Items,
brandName: String) {
self.cart = cart
self.brandName = brandName
}
}
在 HomeViewController 中,在 addActionHandler 下将所选项目附加到购物车
cell.addActionHandler = { (option: Int) in
print("Option selected = \(option)")
item.selectedOption = option
tray.append(Tray(cart: item, brandName: "<Brand Name>"))
}
将托盘[]传递给 CartViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? CartViewController {
vc.items = self.selectedItem
vc.tray = self.tray
}
}
在 CartViewController 中,将项目传递给 cartCell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell", for: indexPath) as! CartCell
let cart = tray[indexPath.row]
cell.configure(withItems: cart.cart)
return cell
}