0

У меня есть 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
    }

Теперь мне нужно что если в данной секции в которой нету продукта она не отображалась а скрывалась ! На данный момент это выглядит так введите сюда описание изображения

Как видите на картинке что присутствует только Топпинги в которых есть хлеб ! Как мне скрыть другие секции так как в них нету продуктов

1 ответ 1

0

Может у кого то будет такая же проблема с простым методом то я решил этот вопрос и вот как он выглядит, элементарно

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if sectionedProducts[section].isEmpty {
        return nil
    }
    return sections[section]
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.