![](/img/trans.png)
[英]how to arrange tableview cells into sections alphabetically with custom header cell?
[英]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: {$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
属性
在进入视图 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
数组来形成您的自定义标题,然后在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
}
它可以简单地通过
为购物车中的所有产品创建一个列表(产品数组):创建一个<SET>
并遍历所有选择的产品并将其品牌添加到<SET>
对于<SET>
中的每个品牌,创建一个过滤掉该品牌所有产品的谓词,您也可以对其进行Filter
。 将结果存储在单独的品牌数组中,一旦处理完成,将 TableView 中的列数设置为过滤后创建的 arrays 的数量,并将每个部分中的行数设置为每个数组的计数
希望这可以帮助 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.