ios - 我将如何使用闭包传递数据?

标签 ios swift uitableview closures segue

我正在尝试使用我的 HomeCell 中的闭包 (addActionHandler) 将数据从我的 HomeViewController 中的单元格传递到我的 CartViewController。

我之前的工作是在 HomeVC 的 cellForRowAt 中使用 Tray.currentCart.cartItems.append(item) 传递数据。

但是由于我通过删除 static let currentCart = Tray() 修改了我的 Tray 类,我似乎无法获得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
}

最佳答案

像下面这样设置类 Tray

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>"))
}

将 tray[] 传递给 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
}

关于ios - 我将如何使用闭包传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59008901/

相关文章:

ios - 无法在没有参数的情况下调用类型 'CustomDetailView' 的初始值设定项

ios - SpriteKit : didBeginContact being called non-stop on iPad

ios - 为单元格的 subview 实现不同的用户交互功能

objective-c - 设置 AVPlayerLayer VideoGravity 适用于模拟器,不适用于 iPad

ios - 你如何使用 Swift 从 PDF 中提取图像?

ios - 如何在重新排序期间更新 UITableViewCell

ios - 单击更改 BarButtonItem 图标

ios - 滑动表格单元格以在 Swift 中执行操作

ios - 使用 Swift 在 UITableview 中对 Backendless 数据进行分组和排序

IOS:多个数组显示在单个 Tableview 上