ios - 如何将项目排列成表格 View 中的部分?

标签 ios swift sorting dynamic tableview

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

因为我所做的只是在购物车中按品牌组织单元格,这类似于数据从 HomeVC 传递到购物车后我们手机中联系人的排列方式

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

enter image description here

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
     } 
}

最佳答案

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

在进入 View 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()
}

使用 brands 数组形成您的自定义 header ,然后在 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
}

关于ios - 如何将项目排列成表格 View 中的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58853099/

相关文章:

ios - 如何复制ios日历

php - 返回数组时,PHP 中的 json_decode 是否保证保留元素的顺序?

iOS/iphone 6 中的 html 5 音频标签不再在受密码保护的目录中工作

ios - 如何为 UIBezierPath 上的填充颜色变化设置动画?

ios - 获取所有可供选择的文件和文件夹

ios - 在 TabBarController 中打开 ViewController 处理推送通知

sql - Mysql sql 如何根据区域设置对表进行排序?

php - 按最高值对 JSON 进行排序 PHP

ios - 核心数据不持久保存对象

ios - 将 Apple Emoji(字符串)转换为 UIImage