如何使用自定义标题将单元格分成多个部分?

我想做的是按照其品牌将单元格分成几部分

到目前为止,我所能做的就是从HomeVC传递选定项目的数据以填充CartVC的单元格

我正在尝试按品牌分开各个部分,品牌数据是模型 Items 类(名称,品牌,imageUrl,价格和重量)的一部分并且Items类从CloudFirestore检索数据以填充HomeVC的单元格

当传递到CartVC时,如何将单元按其品牌分为几部分。

到目前为止,我所做的事情似乎都失败了,因为一旦将项目从HomeVC传递到CartVC时,我只会得到一个标头单元格,并且标有我通过的第一个项目的名称 brand 进入CartVC。当我将更多数据传递到CartVC时,当我尝试按其品牌划分所有CartCell时,所有单元格都停留在传递的第一个项目的部分中。

 extension HomeViewController: UITableViewDelegate,UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return itemSetup.count
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell") as? HomeCell else { return UITableViewCell() }

        let item = itemSetup[indexPath.row]
        cell.configure(withItems: item)
        cell.addactionHandler = { (option: Int) in
            print("Option selected = \(option)")
            self.tray.append(Tray(cart: item))
            item.selectedOption = option
        }

        return cell
    }

}

class CartViewController: UIViewController {

    var items: ProductList!
    var sectionmodel: [Sectionmodel] = []
    var tray: [Tray] = []

    var groupedItems: [String: [Tray]] = [:]
    var brandNames: [String] = []

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

         groupedItems = Dictionary(grouping: tray,by: {$0.cart.brand})
         brandNames = groupedItems.map{$0.key}.sorted()
    }

}

extension CartViewController: UITableViewDataSource,UITableViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return tray.count
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CartCell",for: indexPath) as! CartCell

        let cart = tray[indexPath.row]
        cell.configure(withItems: cart.cart)

        return cell
    }

    func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
        let cartHeader = tableView.dequeueReusableCell(withIdentifier: "CartHeader") as! CartHeader

        cartHeader.storeName.text = "Brand: \(tray[section].cart.brand)"
        return cartHeader
    }

    func tableView(_ tableView: UITableView,heightForHeaderInSection section: Int) -> CGFloat {
        return 45
    }
}

class Tray {
    var cart: ProductList!

    init(cart: ProductList) {

        self.cart = cart
    }
}
great_stonezsl321 回答:如何使用自定义标题将单元格分成多个部分?

只需将您的tableview函数设置为,按部分设置就不会有问题

   func numberOfSections(in tableView: UITableView) -> Int {
        return brandNames.count
    }

    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        let brand = brandNames[section]
        return groupedItems[brand]!.count
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cartCell = tableView.dequeueReusableCell(withIdentifier: "CartCell") as! CartCell

        let brand = brandNames[indexPath.section]
        let itemsToDisplay = groupedItems[brand]![indexPath.row]
        cartCell.configure(withItems: itemsToDisplay.cart)

        return cartCell
    }

    func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
        let cartHeader = tableView.dequeueReusableCell(withIdentifier: "CartHeader") as! CartHeader

        let headerTitle = brandNames[section]
        cartHeader.brandName.text = "Brand: \(headerTitle)"

        return cartHeader
    }
本文链接:https://www.f2er.com/3044447.html

大家都在问