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

标签 ios swift iphone uitableview uitableviewsectionheader

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

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

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

当传递到 CartVC 时,我如何能够按品牌将细胞分成部分。

到目前为止,我所做的似乎失败了,因为一旦我将一个项目从 HomeVC 传递到 CartVC,我只会得到一个标题单元格,其中包含我传递的第一个项目的品牌名称进入 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
    }
}

最佳答案

只需设置您的表格 View 功能,就可以按部分进行设置了

   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
    }

关于ios - 如何使用自定义标题将单元格分成几个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59011215/

相关文章:

ios - Swift - 发送到实例的无法识别的选择器

ios - swift : Update UIView using multipeer connectivity

ios压力传感器API

ios - 无法创建配置文件

ios - 从第二个 View Controller 中关闭一组具有自定义动画的 Controller

ios - 在 Swift 上安排一个接一个的任务但是使用 session

ios - 更改 ViewController 时如何保持 UISwitch 状态?

ios - FBSDKLoginKit 在登录和确认页面后不会重定向我的应用程序

iPhone:如何独立于音量设置来大声播放本地通知声音?

ios - Objective C iPhone 编程一个点或形状来跟随你的光标