1

I've been trying to work this out for some little while now.

I've got a double in my array, but it invariably displays as a rounded integer.

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

Here's the tableView:

    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
    }
}

and here's the definition of the field in Xcode:

enter image description here

Whenever I enter a decimal value...

enter image description here

It invariably reverts to integer:

enter image description here

I'm sure it must be blindingly obvious to someone out there, but I'm afraid it isn't to me.

Any ideas?

pastewort
  • 25
  • 3
  • 2
    Where does the `thread.spare` in `let val = Double(thread.spare)` come from? – Alexander Jun 15 '20 at 14:37
  • 1
    You have `spare` in table view code but `spares` in the tuple, I assume it is a typo? Why do you create a new Double from a Double in the cell? How is the number formatter set up? – Joakim Danielson Jun 15 '20 at 15:05
  • "spares" is the column identifier, not the field in the tuple. The number formatter is style: Decimal, Minimum: 0, Maximum: 99.99, Localize Format and Lenient both checked. As mentioned below, In the code, I've also tried .usesGroupingSeparator, .minimumFractionDigits = 0, .maximumFractionDigits = 2, .locale = NSLocale.currentLocale(), yet none of this appears to make any difference to the output. – pastewort Jun 15 '20 at 19:52

1 Answers1

0

This has nothing to do with any of the code you've shown. — You've got a NumberFormatter on your cell. The way a string entered into the field is interpreted as a Double, and the way the Double value is displayed as a string in the interface, depends upon the configuration of that formatter. Configure it to display stuff after the decimal point and that's what it will do.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I tried that, but however I configure it, seemingly, it still shows integers. In the Number Formatter in Attributes Inspector, I set the style to Decimal, and it still shows an integer whatever is entered as Sample Input. I've also tried .usesGroupingSeparator, .minimumFractionDigits = 0, .maximumFractionDigits = 2, .locale = NSLocale.currentLocale() and none of this appears to make any difference to the output. – pastewort Jun 15 '20 at 18:44
  • Finally got it - thanks matt. Changed the Number Formatter Behaviour in Attributes Inspector from Default to Custom, and loads of additional configuration options opened up! – pastewort Jun 16 '20 at 10:26