如何将项目排列成表格视图中的部分?

How to arrange items into sections in a tableview?

我能说的最简单的方法是如何在 CartVC 中按品牌将单元格排列成多个部分

因为我所做的只是在购物车中按品牌组织单元格 类似于数据从HomeVC

代码确实成功地将数据从 HomeVC 传递到 CartVC,但是单元格仍然没有按品牌名称排列

extension HomeController: 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)

        // passes data to the Cart CartVC when ATC Btn is pressed in each cell
        cell.addActionHandler = { (option: Int) in
            print("Option selected = \(option)")
            Tray.currentCart.cartItems.append(item)
            item.selectedOption = option
        }

        return cell
    }
}

import UIKit

class CartViewController: UIViewController {

    var items: Items!

    var setUp: [Items] = []
    var groupedItems: [String: [Items]] = [:]
    var brands: [String] = []

    @IBOutlet weak var cartTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        cartTableView.dataSource = self
        cartTableView.delegate = self

        groupedItems = Dictionary(grouping: setUp, by: {[=11=].brand})
        brands = groupedItems.map{[=11=].key}.sorted()
    }

}

extension CartViewController: UITableViewDataSource, UITableViewDelegate {

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

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

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


        let brand = brands[indexPath.section]
        let itemToDisplay = groupedItems[brand]![indexPath.row]

        // code that currently transfers data to the cells when ATC is pressed
        let cart = Tray.currentCart.cartItems[indexPath.row]
        cell.configure(withItems: cart)

        return cell
    }

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

        let headerTitle = brands[section]

        return cartHeader
    }

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

它可以简单地通过

实现
  1. 为购物车中的所有产品创建一个所有唯一品牌的列表(产品数组):创建一个 <SET> 并遍历所有选定的产品并将其品牌添加到 <SET>

  2. 对于<SET>中的每个品牌,创建一个谓词过滤掉该品牌的所有产品,您也可以为它用户Filter。将结果存储在单独的品牌数组中,处理完成后,将 TableView 中的列数设置为过滤后创建的数组数,并将每个部分中的行数设置为每个数组的计数

希望这对您有所帮助!

无法按照此处的所有代码,但假设您有一个数组 items,它是 Item 的数组,其中每个 item 都有一个 .brand: String 属性

进入视图控制器后,设置要显示的数据:

var items: [Item] //populated in segue
var groupedItems: [String: [Items]] =[:]
var brands: [String] = []

override viewDidLoad() {
  super.viewDidLoad()
  // any other stuff
  groupedItems = Dictionary(grouping: items, by: {[=10=].brand})
  brands = groupedItems.map{[=10=].key}.sorted()
}

使用brands数组组成您的自定义headers然后在cellForRowAt中根据indexPath参数检索您需要显示的项目


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

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

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
   let headerTitle = brands[section]
   // create and configure the header view
   // the headerTitle contains the 'brand' string for the header
}

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

   // retrieve the relevant item to display...
   // 1. use the section to retrieve the specific brand name string for the section from the brands array
   // 2. then use that that to retrieve the appropriate items array from the dictionary
   // 3. the use indexPath.row to retrieve the specific item for the cell

   let brand = brands[indexPath.section]
   let itemToDisplay = grouped[brand]![indexPath.row]

   //configure cell with itemToDisplay...

   return cell
}