我有 3 个部分,根据其类型添加产品
var productsList = BasketModel.shared.records
var sections: [String] = ["Блюда","Топинги","Напитки"]
var sectionedProducts: [[Products]] = []
override func viewDidLoad() {
super.viewDidLoad()
choiceBtn.layer.cornerRadius = 20
choiceBtn.layer.masksToBounds = true
basketTableView.delegate = self
basketTableView.dataSource = self
orderButton.layer.cornerRadius = 20
orderButton.layer.masksToBounds = true
if !BasketModel.shared.records.isEmpty {
fullBasketview.isHidden = false
sections.forEach { section in
let itemsInSection = productsList.filter({$0.type == section})
sectionedProducts.append(itemsInSection)
for item in itemsInSection {
totalPrice += item.price
totalPriceLabel.text = "\(totalPrice) Руб."
}
}
} else {
sectionedProducts.removeAll()
fullBasketview.isHidden = true
}
}
@IBAction func unwindBasketVC(for unwindSegue: UIStoryboardSegue) {}
}
extension BasketVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !productsList.isEmpty {
return sectionedProducts[section].count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BasketCell", for: indexPath) as! BasketCell
// let basket = BasketModel.shared.records
cell.basketImg.image = sectionedProducts[indexPath.section][indexPath.row].img
cell.basketName.text = sectionedProducts[indexPath.section][indexPath.row].name
cell.basketPrice.text = "\(String(describing: sectionedProducts[indexPath.section][indexPath.row].price)) Руб."
return cell
}
现在我需要,如果在没有产品的这个部分中,它不是显示而是隐藏的!目前它看起来像这样
正如你在图片中看到的,只有里面有面包的浇头!我如何隐藏其他部分,因为其中没有产品
也许有人会用一个简单的方法遇到同样的问题,然后我解决了这个问题,这就是它的外观,基本的