Swift 如何添加单元格';s在特定章节下
我正在解析json文件这是我的文件:Swift 如何添加单元格';s在特定章节下,swift,uitableview,Swift,Uitableview,我正在解析json文件这是我的文件: var jsonCode = """ [ { "productName": "Scrap", "prices": [ { "priceName": "HMS I/II 80:20, CFR Turkey, $/mt",
var jsonCode =
"""
[
{
"productName": "Scrap",
"prices": [
{
"priceName": "HMS I/II 80:20, CFR Turkey, $/mt",
"minMaxPrice": "$ 325.0 - 330.0",
"dailyAvgPrice": "$ 327.5",
"dailyChangePercentage": "0.0",
"fullName": "Scrap - HMS I/II 80:20, CFR Turkey, $/mt"
},
{
"priceName": "Shredded, CFR Turkey, $/mt",
"minMaxPrice": "$ 330.0 - 335.0",
"dailyAvgPrice": "$ 332.5",
"dailyChangePercentage": "0.0",
"fullName": "Scrap - Shredded, CFR Turkey, $/mt"
}
]
},
{
"productName": "Alloy",
"prices": [
{
"priceName": "Nickel, $/mt",
"minMaxPrice": "$ 15690.0 - null",
"dailyAvgPrice": "$ 15690.0",
"dailyChangePercentage": "-0.35",
"fullName": "Alloy - Nickel, $/mt"
}
]
}
]
"""
我为take json数据创建了struct。这是我的结构:
import Foundation
// MARK: - ProductElement
struct Product: Codable {
let productName: String
let prices: [Price]
}
// MARK: - Price
struct Price: Codable {
let priceName: String?
let minMaxPrice, dailyAvgPrice, dailyChangePercentage, fullName: String
}
我需要在tableView中创建这些变量
var fetchedPrices = [Product]()//This is the array which I decode my json
var sections = [String]() //This is for tableView Sections
var itemsInSection = [[Price]]() // This is for section items
我试着这样解码:
sections = []
let jsonData = jsonCode.data(using: .utf8)!
self.fetchedPrices = try! JSONDecoder().decode([Product].self, from: jsonData)
self.fetchedPrices.forEach { (Product) in
self.sections.append(Product.productName)
Product.prices.forEach { (priceInfo) in
self.itemsInSection.append([priceInfo])
}
self.tableView.reloadData()
}
这是我所有的tableView方法:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemsInSection[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "priceCell",for: indexPath) as! priceListCellTableViewCell
print(itemsInSection[indexPath.section][indexPath.row])
print(itemsInSection.count)
//这是print语句的输出。
//价格(价格名称:可选(“切碎,成本加运费土耳其,$/mt”)、minMaxPrice:$330.0-335.0、dailyAvgPrice:$332.5、dailyChangePercentage:$0.0、全名:“废料切碎,成本加运费土耳其,$/mt”)
三,
为什么我不能在特定部分添加项目?看起来是这样的:
因为您正在为每个价格信息创建一个新数组,而不是为每个产品创建一个价格信息数组。一种方法是将forEach更改为
self.sections.append(Product.productName)
var items = [Price]()
Product.prices.forEach { priceInfo in
items.append(priceInfo)
}
self.itemsInSection.append(items)
太辛苦了
数组fetchedPrices
是一个完美的数据源,但是我建议将其重命名为产品
。根本没有理由创建其他对象并分割数据
var products = [Product]()
解码JSON
let jsonData = Data(jsonCode.utf8)
do {
self.products = try JSONDecoder().decode([Product].self, from: jsonData)
DispatchQueue.main.async {
self.tableView.reloadData()
}
} catch { print(error) }
将数据源方法更改为
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return products[section].productName
}
func numberOfSections(in tableView: UITableView) -> Int {
return products.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return products[section].prices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "priceCell",for: indexPath) as! priceListCellTableViewCell
let price = products[indexPath.section].prices[indexPath.row]
cell.cellHeader.text = price.priceName
cell.dailyChange.text = price.dailyChangePercentage
cell.minMaxPrice.text = price.minMaxPrice
cell.dailyAvgPrice.text = price.dailyAvgPrice
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return products[section].productName
}
func numberOfSections(in tableView: UITableView) -> Int {
return products.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return products[section].prices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "priceCell",for: indexPath) as! priceListCellTableViewCell
let price = products[indexPath.section].prices[indexPath.row]
cell.cellHeader.text = price.priceName
cell.dailyChange.text = price.dailyChangePercentage
cell.minMaxPrice.text = price.minMaxPrice
cell.dailyAvgPrice.text = price.dailyAvgPrice
return cell
}