用字典数组填充 Tableview 并按部分分隔
Fill the Tableview with Array of Dictionary and separating by section
如何从字典数组中填充表格视图并将其按部分分隔
我从 Firebase 接收数据,有时可能为空
2 或 3 个项目。数据类型是字典数组 [[String:Int]]
如果它有一个项目,它可以是例如:
var selectedVariation:[["ice": 20$, "cold": 30$], ["no Choco": 0$, "with Choco": 40$]]
在这种情况下,我应该在 nonamed 2 部分中分隔 tableview。
第一部分应包含 ["ice": 20$, "cold": 30$] 和第二部分 ["no Choco": 0$, "with Choco": 40$]
使用下面的代码,我可以只填充没有部分的表格视图,应该手动编写 numberOfRows
var selectedVariation: [[String:Int]]?
var keysArray: [String] = []
var valueArray: [Int] = []
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return selectedVariation?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "VariationCell", for: indexPath) as! VariationTableViewCell
for section in selectedVariation! {
for (key, value) in section.sorted(by: {[=11=].1 < .1}) {
keysArray.append(key)
valueArray.append(value)
}
}
cell.variationName.text = keysArray[indexPath.row]
cell.variationPrice.text = "+" + String(valueArray[indexPath.row])
return cell
}
如果我能在每个部分只勾选一个项目的话,还有额外的好处:)
最好的解决方案是将字典映射到自定义结构,但如果您想保留此数据模型,table 查看数据源方法是
var selectedVariation = [["ice": 20, "cold": 30], ["no Choco": 0, "with Choco": 40]]
func numberOfSections(in tableView: UITableView) -> Int {
return selectedVariation.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
selectedVariation[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "VariationCell", for: indexPath) as! VariationTableViewCell
let section = selectedVariation[indexPath.section]
let key = section.keys.sorted()[indexPath.row]
let value = section[key]!
cell.variationName.text = key
cell.variationPrice.text = "+" + String(value)
return cell
}
注:func numberOfSectionsInTableView(tableView: UITableView) -> Int
是Swift2
如何从字典数组中填充表格视图并将其按部分分隔
我从 Firebase 接收数据,有时可能为空 2 或 3 个项目。数据类型是字典数组 [[String:Int]] 如果它有一个项目,它可以是例如:
var selectedVariation:[["ice": 20$, "cold": 30$], ["no Choco": 0$, "with Choco": 40$]]
在这种情况下,我应该在 nonamed 2 部分中分隔 tableview。 第一部分应包含 ["ice": 20$, "cold": 30$] 和第二部分 ["no Choco": 0$, "with Choco": 40$]
使用下面的代码,我可以只填充没有部分的表格视图,应该手动编写 numberOfRows
var selectedVariation: [[String:Int]]?
var keysArray: [String] = []
var valueArray: [Int] = []
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return selectedVariation?.count ?? 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "VariationCell", for: indexPath) as! VariationTableViewCell
for section in selectedVariation! {
for (key, value) in section.sorted(by: {[=11=].1 < .1}) {
keysArray.append(key)
valueArray.append(value)
}
}
cell.variationName.text = keysArray[indexPath.row]
cell.variationPrice.text = "+" + String(valueArray[indexPath.row])
return cell
}
如果我能在每个部分只勾选一个项目的话,还有额外的好处:)
最好的解决方案是将字典映射到自定义结构,但如果您想保留此数据模型,table 查看数据源方法是
var selectedVariation = [["ice": 20, "cold": 30], ["no Choco": 0, "with Choco": 40]]
func numberOfSections(in tableView: UITableView) -> Int {
return selectedVariation.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
selectedVariation[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "VariationCell", for: indexPath) as! VariationTableViewCell
let section = selectedVariation[indexPath.section]
let key = section.keys.sorted()[indexPath.row]
let value = section[key]!
cell.variationName.text = key
cell.variationPrice.text = "+" + String(value)
return cell
}
注:func numberOfSectionsInTableView(tableView: UITableView) -> Int
是Swift2