如何在页脚中使用 segue 将标签数据传递到不同的部分?
How to use segue in footer to pass label data to different sections?
当按下页脚单元格中的按钮时,我已经成功地将项目VC的标签数据传递给费用VC
但是当我试图将表格视图中的部分标签传递给费用时,我似乎无法让它工作VC
我正在尝试将小计从项目VC 页脚传递到不同部分的 FeesViewController 标签,而不是只传递一个部分的数据
我已经成功地从页脚传递了数据(按下按钮时)。它已经成功地将 totalPriceLbl 从 BrandFooter 传递到 Fees VC 但不能将数据从正确的部分传递到 FeesVC 如果这有意义的话我怎么能按下按钮时将正确的标签数据传递给费用
我觉得我真的很接近我的解决方案我就在某个地方
我认为问题出在 viewForFooterInSection 中,它将小计(值)从该部分传递给费用VC
提前感谢您提供的任何帮助
class ItemsViewController: UIViewController {
var brandItems: [BrandItem] = []
var groupedBrandItems: [String: [BrandItem]] = [:]
var brandSectionTitle: [String] = []
var selectedLabel: String? // Populates label data in FeesVC
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
groupedBrandItems = Dictionary(grouping: brandItems, by: {[=10=].products.brandName})
brandSectionTitle = groupedBrandItems.map{[=10=].key}.sorted()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}if let vc = segue.destination as? FeesViewController {
vc.stringPassed = selectedLabel! // Populates label data in FeesVC
}
}
}
extension ItemsViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return brandSectionTitle.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let brand = brandSectionTitle[section]
return groupedBrandItems[brand]!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let brandCell = tableView.dequeueReusableCell(withIdentifier: "BrandCell") as! BrandCell
let brand = brandSectionTitle[indexPath.section]
let brandItemsToDisplay = groupedBrandItems[brand]![indexPath.row]
brandCell.configure(withCartItems: brandItemsToDisplay.products)
return brandCell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let brandHeader = tableView.dequeueReusableCell(withIdentifier: "BrandHeader") as! BrandHeader
let headerTitle = brandSectionTitle[section]
brandHeader.brandName.text = "Brand: \(headerTitle)"
return brandHeader
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let brandFooter = tableView.dequeueReusableCell(withIdentifier: "BrandFooter") as! BrandFooter
let brand = brandSectionTitle[section]
let arrAllItems = groupedCartItems[brand]!
var subtotal: Float = 0
for item in arrAllItems {
if item.products.selectedOption == 1 {
subtotal = subtotal + (Float(item.products.price) * Float(item.products.count))
}
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
let total = numberFormatter.string(from: NSNumber(value: Float(subtotal)))
brandFooter.totalPrice.text = String(Total!)
//trying to pass each section subtotal to FeesVC
self.selectedLabel = "\(subtotal)" // passes code to FeesVC
return brandFooter
}
}
class FeesViewController: UIViewController {
@IBOutlet weak var feesView: UIView!
@IBOutlet weak var subtotalLbl: UILabel!
@IBOutlet weak var salesTaxLbl: UILabel!
@IBOutlet weak var totalLbl: UILabel!
var stringPassed = String() // Populates label data in FeesVC
override func viewDidLoad() {
super.viewDidLoad()
let tax = Float(stringPassed)! * Float(0.0825)
let total = Float(stringPassed)! + tax
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
let subTotal = numberFormatter.string(from: NSNumber(value: Float(stringPassed)!))
let salesTax = numberFormatter.string(from: NSNumber(value: Float(tax)))
let overallTotal = numberFormatter.string(from: NSNumber(value: Float(total)))
subtotalLbl.text = subTotal
salesTaxLbl.text = "(\(String(describing: salesTax!)))"
totalLbl.text = "(\(String(describing: overallTotal!)))"
}
@IBAction func returnButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
print("Close Taxes and Fees")
}
}
更新 :
当我尝试 post 每当按下更多信息按钮时每个部分的小计
时,我一直在费用 VC 中得到相同的小计和计算
self.selectedLabel = "\(subtotal)"
viewForFooterInSection 中的这行代码在为一个部分按下按钮时成功地传递了小计,但是当在相应的页脚中按下按钮时它并没有传递每个部分的小计
假设我从你的问题中了解到你需要将小计传递给其他 VC。
你做错的是将小计保存在 footerForSection 的一个变量中,这个变量将包含最后一个页脚部分的最后小计(这就是你的代码中发生的事情)
self.selectedLabel = "\(subtotal)" // passes code to FeesVC
编辑
因此,在您的场景中,您需要将小计保存在一个数组中,当您点击一个节页脚时,在点击时获取节的索引并使用该数组获取数据并将其传递给其他 VC
首先声明一个双精度数组来存储你的每一个小计。
var subTotalArray[Double] = [Double]()
然后,当您在 viewForFooterInSection 函数中计算小计时,像这样初始化您创建的数组(根据您的代码和变量名称更改):
subTotalArray[section] = subtotal
// use the local variable "section" to iterate
// and assign the subtotal to the correct section footer
Next 要确定按下哪个信息按钮以获得正确的小计数据,您可以在 viewForFooterInSection 函数中配置更多信息按钮的标签 属性 .像这样分配标签:
moreInfo.tag = section
最后 有了所有这些变量,我们就可以使用连接到您的更多信息按钮的处理函数正确分配、确定和收集每个单独的小计。
func handleMoreInfo(sender: UIButton) {
selectedLabel = subTotalArray[sender.tag]
}
当按下页脚单元格中的按钮时,我已经成功地将项目VC的标签数据传递给费用VC
但是当我试图将表格视图中的部分标签传递给费用时,我似乎无法让它工作VC
我正在尝试将小计从项目VC 页脚传递到不同部分的 FeesViewController 标签,而不是只传递一个部分的数据
我已经成功地从页脚传递了数据(按下按钮时)。它已经成功地将 totalPriceLbl 从 BrandFooter 传递到 Fees VC 但不能将数据从正确的部分传递到 FeesVC 如果这有意义的话我怎么能按下按钮时将正确的标签数据传递给费用
我觉得我真的很接近我的解决方案我就在某个地方
我认为问题出在 viewForFooterInSection 中,它将小计(值)从该部分传递给费用VC
提前感谢您提供的任何帮助
class ItemsViewController: UIViewController {
var brandItems: [BrandItem] = []
var groupedBrandItems: [String: [BrandItem]] = [:]
var brandSectionTitle: [String] = []
var selectedLabel: String? // Populates label data in FeesVC
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
groupedBrandItems = Dictionary(grouping: brandItems, by: {[=10=].products.brandName})
brandSectionTitle = groupedBrandItems.map{[=10=].key}.sorted()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}if let vc = segue.destination as? FeesViewController {
vc.stringPassed = selectedLabel! // Populates label data in FeesVC
}
}
}
extension ItemsViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return brandSectionTitle.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let brand = brandSectionTitle[section]
return groupedBrandItems[brand]!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let brandCell = tableView.dequeueReusableCell(withIdentifier: "BrandCell") as! BrandCell
let brand = brandSectionTitle[indexPath.section]
let brandItemsToDisplay = groupedBrandItems[brand]![indexPath.row]
brandCell.configure(withCartItems: brandItemsToDisplay.products)
return brandCell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let brandHeader = tableView.dequeueReusableCell(withIdentifier: "BrandHeader") as! BrandHeader
let headerTitle = brandSectionTitle[section]
brandHeader.brandName.text = "Brand: \(headerTitle)"
return brandHeader
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let brandFooter = tableView.dequeueReusableCell(withIdentifier: "BrandFooter") as! BrandFooter
let brand = brandSectionTitle[section]
let arrAllItems = groupedCartItems[brand]!
var subtotal: Float = 0
for item in arrAllItems {
if item.products.selectedOption == 1 {
subtotal = subtotal + (Float(item.products.price) * Float(item.products.count))
}
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
let total = numberFormatter.string(from: NSNumber(value: Float(subtotal)))
brandFooter.totalPrice.text = String(Total!)
//trying to pass each section subtotal to FeesVC
self.selectedLabel = "\(subtotal)" // passes code to FeesVC
return brandFooter
}
}
class FeesViewController: UIViewController {
@IBOutlet weak var feesView: UIView!
@IBOutlet weak var subtotalLbl: UILabel!
@IBOutlet weak var salesTaxLbl: UILabel!
@IBOutlet weak var totalLbl: UILabel!
var stringPassed = String() // Populates label data in FeesVC
override func viewDidLoad() {
super.viewDidLoad()
let tax = Float(stringPassed)! * Float(0.0825)
let total = Float(stringPassed)! + tax
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
let subTotal = numberFormatter.string(from: NSNumber(value: Float(stringPassed)!))
let salesTax = numberFormatter.string(from: NSNumber(value: Float(tax)))
let overallTotal = numberFormatter.string(from: NSNumber(value: Float(total)))
subtotalLbl.text = subTotal
salesTaxLbl.text = "(\(String(describing: salesTax!)))"
totalLbl.text = "(\(String(describing: overallTotal!)))"
}
@IBAction func returnButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
print("Close Taxes and Fees")
}
}
更新 :
当我尝试 post 每当按下更多信息按钮时每个部分的小计
时,我一直在费用 VC 中得到相同的小计和计算self.selectedLabel = "\(subtotal)"
viewForFooterInSection 中的这行代码在为一个部分按下按钮时成功地传递了小计,但是当在相应的页脚中按下按钮时它并没有传递每个部分的小计
假设我从你的问题中了解到你需要将小计传递给其他 VC。
你做错的是将小计保存在 footerForSection 的一个变量中,这个变量将包含最后一个页脚部分的最后小计(这就是你的代码中发生的事情)
self.selectedLabel = "\(subtotal)" // passes code to FeesVC
编辑
因此,在您的场景中,您需要将小计保存在一个数组中,当您点击一个节页脚时,在点击时获取节的索引并使用该数组获取数据并将其传递给其他 VC
首先声明一个双精度数组来存储你的每一个小计。
var subTotalArray[Double] = [Double]()
然后,当您在 viewForFooterInSection 函数中计算小计时,像这样初始化您创建的数组(根据您的代码和变量名称更改):
subTotalArray[section] = subtotal
// use the local variable "section" to iterate
// and assign the subtotal to the correct section footer
Next 要确定按下哪个信息按钮以获得正确的小计数据,您可以在 viewForFooterInSection 函数中配置更多信息按钮的标签 属性 .像这样分配标签:
moreInfo.tag = section
最后 有了所有这些变量,我们就可以使用连接到您的更多信息按钮的处理函数正确分配、确定和收集每个单独的小计。
func handleMoreInfo(sender: UIButton) {
selectedLabel = subTotalArray[sender.tag]
}