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

时间:2018-10-22 21:33:52

标签: ios swift uitableview

我试图在UITableView中显示[String:[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个Eduardo-> [45.5,75.799999999999997,53.5]

2 Joao-> [55.5,43.799999999999997,88.5]

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

我知道我应该使用类似String(format:“%.2f”,...)的东西,但是在哪里以及如何使用?

3 个答案:

答案 0 :(得分:0)

一个简单的解决方案是在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 :(得分:0)

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
        }

答案 2 :(得分:0)

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

更简单的是更改行

let notas = objectArray[indexPath.row].sectionObjects

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

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

然后,表达式“(notas)`将显示带有开括号和闭括号以及逗号分隔符的字符串数组。