Ios 在单元格中按下按钮时,如何将数据从一个表视图传递到另一个表视图?
我正在尝试将数据从ViewController传递到我的CartViewController。ViewController中的单元格有3个按钮(OptionBTN),每个按钮上方都有一个价格和重量标签 我想做的是让选择的BTN在按下ATC按钮后将标签数据传递到其上方 单元格中的ATC按钮将图像、名称、类别和选项BTN数据的数据传递给CartViewController单元格(CartCell) 当按下ATC以在单元格中用所选选项BTN数据(价格和重量)显示所选项目名称、图像和类别时,如何将所选数据传递给CartVC 我还使用CloudFireStore发布数据来填充我的VC单元格Ios 在单元格中按下按钮时,如何将数据从一个表视图传递到另一个表视图?,ios,swift,uitableview,google-cloud-firestore,segue,Ios,Swift,Uitableview,Google Cloud Firestore,Segue,我正在尝试将数据从ViewController传递到我的CartViewController。ViewController中的单元格有3个按钮(OptionBTN),每个按钮上方都有一个价格和重量标签 我想做的是让选择的BTN在按下ATC按钮后将标签数据传递到其上方 单元格中的ATC按钮将图像、名称、类别和选项BTN数据的数据传递给CartViewController单元格(CartCell) 当按下ATC以在单元格中用所选选项BTN数据(价格和重量)显示所选项目名称、图像和类别时,如何将所选数
如果想法是让firebase处理所有数据,那么数据应该通过firebase,而不是通过viewController。ie您的Firebase db应该有一个
项目
集合(或者每个商店
)并且您的用户
应该有一个购物车
集合。当用户点击“添加到购物车”按钮时,您将有问题的项目添加到firebase中的该用户购物车集合中,然后显示cartViewController。cartViewController应订阅当前用户上的购物车集合,然后从该firebase集合填充其tableview
TLDR firebase应用程序的典型设计是firebase DB是应用程序的真实来源,因此您应该将所有更改写入firebase,然后只需在应用程序的其他位置观察这些集合。这还可以确保,如果用户在另一台设备上编辑购物车,它将在iOS设备上使用新购物车项目更新自身。您可以在闭包中传递值
因此,在Cell类中,您可以将闭包变量更改为:
var addActionHandler: ((Int) -> Void)?
然后,在addToCart按钮操作中,可以看到以下内容:
@IBAction func atcBtn(_ sender: UIButton) {
// pass back the user selected values
var i = 0
switch lastSelectedButton {
case optionBtn1:
i = 1
case optionBtn2:
i = 2
default:
i = 3
}
self.addActionHandler?(i)
}
创建Items类的.selectedOption属性时,应添加一个(Int类型)。您可以使用它来跟踪用户的选择
在单元格中修改cellForAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell else { return UITableViewCell() }
// use var to make item mutable
var item = itemSetup[indexPath.row]
cell.configure(withItem: item)
cell.addActionHandler = { (option: Int) in
print("Option selected = \(option)")
item.selectedOption = option
Cart.currentCart.items.append(item)
}
return cell
}
在CartCell中,您可以制作如下标签:
if items.selectedOption == 1 {
lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
lblWeight.text = "\(items.weight1)"
} else if items.selectedOption == 2 {
lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
lblWeight.text = "\(items.weight2)"
} else if items.selectedOption == 3 {
lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
lblWeight.text = "\(items.weight3)"
}
在ViewController
中,当您调用cell.configure(withItem:itemSetup[indexPath.row])
时,是否忘记了设置单元格的委托cell.delegate=self
。因为我看不到任何地方可以这样做。上次编辑时我忘了添加它,但它现在在那里,并且已经存在于我的代码中。刚刚更新了我的代码,我想我离解决方案更近了,只是不知道从那里走到哪里,如果你不介意的话look@Josh这是一个很好的主意,当事情设置好时,它会使事情对我更好地工作,但我不想这样做,但我只是想在单元格中按下ATC btn以及所选数据选项后,将数据传递给CartVC。当我按下一个选项按钮,ATC btn将数据发送到所选单元格的CartVC单元格时,我一直在尝试使这项功能正常工作。我刚刚更新了代码,它现在将数据从VC传递到CartVC,但我如何才能使它传递所选选项btn(重量和价格)的数据将所选数据传递给CartVC
var addActionHandler: ((Int) -> Void)?
@IBAction func atcBtn(_ sender: UIButton) {
// pass back the user selected values
var i = 0
switch lastSelectedButton {
case optionBtn1:
i = 1
case optionBtn2:
i = 2
default:
i = 3
}
self.addActionHandler?(i)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell else { return UITableViewCell() }
// use var to make item mutable
var item = itemSetup[indexPath.row]
cell.configure(withItem: item)
cell.addActionHandler = { (option: Int) in
print("Option selected = \(option)")
item.selectedOption = option
Cart.currentCart.items.append(item)
}
return cell
}
if items.selectedOption == 1 {
lblSubTotal.text = "$\(formatter.string(for: items.price1)!)"
lblWeight.text = "\(items.weight1)"
} else if items.selectedOption == 2 {
lblSubTotal.text = "$\(formatter.string(for: items.price2)!)"
lblWeight.text = "\(items.weight2)"
} else if items.selectedOption == 3 {
lblSubTotal.text = "$\(formatter.string(for: items.price3)!)"
lblWeight.text = "\(items.weight3)"
}