Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用闭包传递数据?_Ios_Swift_Uitableview_Closures_Segue - Fatal编程技术网

Ios 如何使用闭包传递数据?

Ios 如何使用闭包传递数据?,ios,swift,uitableview,closures,segue,Ios,Swift,Uitableview,Closures,Segue,我试图使用HomeCell中的closure addActionHandler将数据从HomeViewController中的单元格传递到CartViewController 我之前的工作是使用HomeVC的cellForRowAt中的Tray.currentCart.cartItems.appenditem传递数据 但是,由于我通过删除静态let currentCart=Tray修改了我的Tray类,我似乎无法让HomeVC中的单元格将数据传递给CartVC 在Home单元格中按下ATCBtn

我试图使用HomeCell中的closure addActionHandler将数据从HomeViewController中的单元格传递到CartViewController

我之前的工作是使用HomeVC的cellForRowAt中的Tray.currentCart.cartItems.appenditem传递数据

但是,由于我通过删除静态let currentCart=Tray修改了我的Tray类,我似乎无法让HomeVC中的单元格将数据传递给CartVC

在Home单元格中按下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 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中,将项目传递给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
}

单击atcBtn,打开带有所选项目的cartViewController?。这是要求吗?atcBtn不打开CartVC只传递所选数据项以填充CartVC单元格,HomeVC导航栏中的CartBtn打开CARTVCTARY:[TARY]=[]CartViewController中的CARTVCTARY应该从HomeViewController填充,对吗?是的,它用于从HomeVC填充CartVC中的单元格。var itemSetup:[Items]=[]填充HomeVC的单元格。Items类从cloudFirestore提取数据。我只是没有将Firestore代码放在HomeVC中的问题NumberOfSections始终为1?。如果是,则可以避免使用托盘[indexPath.row].cartItems[indexPath.section]。在HomeVC中创建一个托盘[],并传递给CartVC。除非需要,否则应避免使用包含项和项[]的类托盘。我得到的唯一问题是CartVC cellForRowAt单元格中。configurewithItems:cart的错误代码为“无法将类型为“托盘”的值转换为预期的参数类型“项”!我成功了!!:在牢房里,我做了自己的事。tray.appendTraycart:item,brandName:在CartVC CellForRow中我做了这个单元格。配置WithItems:cart.cart这样做可以吗?或者,当数据在cellscell.configurewithItems:cart.cart中传递时,它会在以后继续编写代码时给我带来问题,因为我将转换它以将其分成多个部分。这是正确的,解决方案已更新。这不会产生问题。如果需要添加更多节,请相应地更改类托盘。我很高兴为您找到了解决方案:非常感谢@Dili oh好的,我不知道我必须修改tray类以向CartVC添加部分。如果有更多部分。创建一个类似于struct SectionModel{var title:String var rowItems:[Tray]}的节模型。并将[SectionModel]用作tableview的数据数组
cell.addActionHandler = { (option: Int) in
    print("Option selected = \(option)")
    item.selectedOption = option
    tray.append(Tray(cart: item, brandName: "<Brand Name>"))
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? CartViewController {
        vc.items = self.selectedItem
        vc.tray = self.tray
    }
}
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
}