如何在表视图中显示字符串和双精度词典



我正在尝试在UITableView中显示[字符串:[Double]]字典,但是当双精度以与原始格式不同的格式出现时,我遇到了意外的行为。这是我的基本代码:

var resdict = ["Joao":[55.5,43.8,88.5],"Carlos":[65.5,45.8,58.5],"Eduardo":[45.5,75.8,53.5]]
struct Objects {
var sectionName : String
var sectionObjects : [Double]
}
var objectArray = [Objects]()

//table
@IBOutlet weak var resultsTable: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as UITableViewCell
let name = objectArray[indexPath.row].sectionName
let notas = objectArray[indexPath.row].sectionObjects
cell.textLabel?.text = "(indexPath.row + 1) (name) -> (notas)"
return cell
} 

在viewDidLoad:

for (key, value) in resdict {
print("(key) -> (value)")
objectArray.append(Objects(sectionName: key, sectionObjects: value))
}

结果是:

1 爱德华多 -> [45.5, 75.7999999999999997, 53.5]

2 若昂 -> [55.5, 43.799999999999997, 88.5]

3 卡洛斯 -> [65.5, 45.799999999999997, 58.5]

我知道我应该应用类似字符串的东西(格式:"%.2f",...(但是在哪里以及如何应用?

正如其他人所说,您可以使用 map 和 join 将双精度数组转换为以"["开头的字符串,具有值,用逗号分隔,然后以"]"结尾

不过,更简单的是改变你的路线

let notas = objectArray[indexPath.row].sectionObjects

let notas = objectArray[indexPath.row]
.sectionObjects
.map { String(format: ,%.2f", $0) }

这会将您的双精度数组转换为具有 2 位小数的字符串数组。

然后表达式"(notas("将显示带有左大括号和右大括号以及逗号分隔符的字符串数组。

一个简单的解决方案是在Objects中添加一个计算属性(实际上是单数Object(,以将Double数组映射到String并用逗号分隔连接它。

struct Objects {
var sectionName : String
var sectionObjects : [Double]
var objectsStringRepresentation : String {
return sectionObjects.map{String(format: "%.2f", $0)}.joined(separator:", ")
}
}
...
let notas = objectArray[indexPath.row].objectsStringRepresentation 

1 您需要将数据传递给对象数组, 2 使用部分设置成绩的标题和行

override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
resdict.forEach({(k,v) in
objectArray.append(Objects(sectionName: k, sectionObjects: v))
})
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.tableView.reloadData()
}
var resdict = ["Joao":[55.5,43.8,88.5],"Carlos":[65.5,45.8,58.5],"Eduardo":[45.5,75.8,53.5]]
struct Objects {
var sectionName : String
var sectionObjects : [Double]
}
var objectArray = [Objects]()

//table
func numberOfSections(in tableView: UITableView) -> Int {
return objectArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectArray[section].sectionObjects.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let name = objectArray[section].sectionName
return name
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as UITableViewCell
let notas = objectArray[indexPath.section].sectionObjects[indexPath.row]
cell.textLabel?.text = "(notas)"
return cell
}

最新更新