简体   繁体   English

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

[英]How to arrange items into sections in a tableview?

Easiest way I can say this is how would I be able to arrange the cells into sections by their brand inside the CartVC我能说的最简单的方法是我如何能够在 CartVC 中按其品牌将单元格排列成部分

Since all Im trying to do is organize the cells by their brand in the Cart similar to how the contacts are arranged in our phones work once data is passed into the cart from the HomeVC由于我所做的只是在购物车中按其品牌组织单元格,这类似于一旦将数据从 HomeVC 传递到购物车后,我们的手机中联系人的排列方式就会起作用

The code does pass data from the HomeVC to the CartVC successfully, But the cells still do not arrange by their brand name该代码确实将数据从 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: {$0.brand})
        brands = 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.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
     } 
}

can't follow all your code on here, but assuming you have an array items which is an array of Item where each item has a .brand: String property无法在此处遵循所有代码,但假设您有一个数组items ,它是Item数组,其中每个item都有一个.brand: String属性

on entering view controller, set up your data to display:在进入视图 controller 时,设置要显示的数据:

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: {$0.brand})
  brands = groupedItems.map{$0.key}.sorted()
}

use the brands array to form your custom headers and then in cellForRowAt retrieve the item you need to display based on the indexPath parameter使用brands数组来形成您的自定义标题,然后在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
}

It could be simply implemented by它可以简单地通过

  1. Create a list all unique brands for all your products in cart(products array): Create a <SET> and iterate through all the products selected and add their brand in <SET>为购物车中的所有产品创建一个列表(产品数组):创建一个<SET>并遍历所有选择的产品并将其品牌添加到<SET>

  2. For every brand in <SET> , create a predicate that filters out all products of the brand, you can also user Filter for it.对于<SET>中的每个品牌,创建一个过滤掉该品牌所有产品的谓词,您也可以对其进行Filter Store the results in separate brand array and once processing is done, set the number of columns in TableView to number of arrays created after filtering, and number of rows in each section to count of each array将结果存储在单独的品牌数组中,一旦处理完成,将 TableView 中的列数设置为过滤后创建的 arrays 的数量,并将每个部分中的行数设置为每个数组的计数

Hope this helps !希望这可以帮助 !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM