Swift: 如何处理 tableView 中带有两位小数的双精度数?

Swift: How do I handle a double with two decimal places in a tableView?

我已经尝试解决这个问题一段时间了。

我的数组中有一个双精度数,但它总是显示为四舍五入的整数。

var dataArray: [(colorNo: String, colour: NSColor, spares: Double)] = []

这是表格视图:

    func numberOfRowsInTableView(aTableView: NSTableView) -> Int {
        return dataArray.count
    }
}

extension ViewController: NSTableViewDelegate {
    func tableView(tableView: NSTableView,
                   viewForTableColumn tableColumn: NSTableColumn?,
                   row: Int) -> NSView? {
        if let column = tableColumn {
            if let cellView = tableView.makeViewWithIdentifier(column.identifier, owner: self) as? NSTableCellView {
                let thread = dataArray[row]
                if column.identifier == "colourNo" {
                    cellView.textField?.stringValue = "\(thread.colourNo)"
                    return cellView
                }
                if column.identifier == "colour" {
                    cellView.textField?.backgroundColor = thread.colour
                    return cellView
                }
                if column.identifier == "spares" {
                    let val = Double(thread.spare)
                    cellView.textField?.doubleValue = val
                    return cellView
                }
                return cellView
            }
        }
        return nil
    }
}

这里是Xcode中字段的定义:

每当我输入小数值时...

它总是恢复为整数:

我敢肯定这对外面的人来说一定是显而易见的,但我恐怕不是。

有什么想法吗?

这与您显示的任何代码都无关。 — 你的手机上有一个 NumberFormatter。输入字段的字符串被解释为 Double 的方式,以及 Double 值在界面中显示为字符串的方式,取决于格式化程序的配置。将其配置为显示小数点后的内容,这就是它的作用。