简体   繁体   中英

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

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

The code does pass data from the HomeVC to the CartVC successfully, But the cells still do not arrange by their brand name

在此处输入图像描述

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

on entering view controller, set up your data to display:

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


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>

  2. For every brand in <SET> , create a predicate that filters out all products of the brand, you can also user Filter for it. 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

Hope this helps !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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