【发布时间】:2020-04-09 13:18:51
【问题描述】:
当按下页脚单元格中的按钮时,我已经成功地将 ItemsVC 的标签数据传递给 Fees VC
但是当我试图将表格视图中的部分的标签传递给 FeesVC 时,我似乎无法让它工作
我试图将 ItemsVC 页脚中的小计传递给不同部分的 FeesViewController 标签,而不是只传递一个部分的数据
我已成功从页脚传递数据(按下按钮时)。它成功地能够将总价格从 BrandFooter 传递给 Fees VC,但如果这有任何意义,则无法将数据从右侧部分传递给 FeesVC 我如何能够将正确的标签数据传递给费用按钮被按下
我觉得我真的很接近我的解决方案,我就在某个地方
我认为问题在于 viewForFooterInSection 将小计(值)从该部分传递到 FeesVC
提前感谢您提供的任何帮助
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: {$0.products.brandName})
brandSectionTitle = groupedBrandItems.map{$0.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")
}
}
更新:
每当按下更多信息按钮时,我尝试发布每个部分的小计时,我一直在 FeesVC 中获得相同的小计和计算
self.selectedLabel = "\(subtotal)"
viewForFooterInSection 中的这行代码在按下某个部分的按钮时成功传递了小计,但是当在相应的页脚中按下按钮时,它并没有传递每个部分的小计
【问题讨论】:
-
所以澄清一下,小计只是从一个单元格的数据计算,而不是得到所有单元格数据的总和?抱歉,试图理解
-
@drfalcoew 我得到了该部分中所有单元格的总和,它更多地显示了 FeesVC 中每个部分的总和,因为我仅获得 FeesVC 中某个部分的总和,很快就会发布一张图片来向您展示我的意思。
-
嗨,看看我的回答
标签: ios swift tableview segue pass-data