Volkan Paksoy Volkan Paksoy - 4 months ago 9
iOS Question

Displaying integer values on SliderRow

I'm trying display the integer values instead of float. I tried the following:

section.append(SliderRow() {
$0.title = "Number of items"
$0.tag = "numberOfItems"
$0.minimumValue = 1
$0.maximumValue = 10
$0.steps = 10
$0.value = 5
$0.onChange { row in
let isInteger = floor(row.value!) == row.value!
if (!isInteger) {
row.value = floor(row.value!)

let formattedString = String(format: "%.0f", row.value!)
row.cell.valueLabel.text = formattedString

formattedString displays the values I want but I'm not able to display them on the screen. I can access all other attributes by row.cell. I change change the text colour, for instance, but not the text. I'm guessing I'm setting the text but it gets overwritten shortly.

Is there a way to make the slider show integer values?



To resolve the issue I cloned the Eureka repository. Turns out I was right about overwriting the values. To test it I set the text to a constant value inside onChange handler, then added print statements in the SliderRow source code in valueChanged function:

    if shouldShowTitle() {

        print("current value: \(valueLabel.text)" )

        valueLabel.text = "\(row.value!)"

        print("updated value: \(valueLabel.text)" )

The output looked like this:

current value: Optional("xyz")

updated value: Optional("3.0")

current value: Optional("xyz")

updated value: Optional("5.0")

So I created myself a SliderRowInt class that uses integers:

public final class SliderRowInt: Row<Int, SliderCellInt>, RowType {

    public var minimumValue: Int = 0
    public var maximumValue: Int = 10
    public var steps: UInt = 20

    required public init(tag: String?) {
        super.init(tag: tag)
    } }

Now I'm using the integer version. Maybe not the most elegant solution but it worked fine for me.

There's a similar issue with the StepperRow which I solved the same way.

I checked in both classes to my cloned repository in case anyone encounters the same problem they can be found here: https://github.com/volkanx/Eureka/