UA-17470720-3

Jump to content


Photo
- - - - -

Formatting numbers when using curly brace binding

cells

  • Please log in to reply
11 replies to this topic

#1 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 25 November 2015 - 09:19 AM

Hello,

I'm working on using interface builder w/ STV for the first time and am running into some issues.

The first issue that I cannot seem to resolve involves using curly brace binding.  I watched the video for UI Customization and what I am trying to achieve is similar to what is mentioned around 2:00.  I have a cell that I added an additional label and using curly brace binding I specify what to have displayed there.  It works like a charm.

The problem is that this value is an NSNumber that I need to display as currency. 

 

Here is what I have tried so far:

    self.tableViewModel.cellActions.didLoadBoundValue = ^NSObject*(SCTableViewCell *cell, NSIndexPath *indexPath, NSObject *value)
    {

        NSString *formattedValue = (NSString *)value;

        if ([cell.boundPropertyName isEqualToString:@"laborCostAmt"]) {
            NSNumber *actualValue = [cell.boundObject valueForKey:cell.boundPropertyName];
            if (actualValue != nil) {
                formattedValue = [[NSNumberFormatter currencyFormatter] stringFromNumber:actualValue];
            }
        }
        return formattedValue;
    };

 

Stepping through the debugger and using "po" this seems to behave as expected.  However the formatted value does not actually display in the cell.  Instead, the float value displays (ex "888.2" displays instead of "$888.20").

 

I also tried using both willConfigure and willDisplay cell actions to grab the SCNumericTextFieldCell, then changing it's numberFormatter property to be NSNumberFormatterCurrencyStyle.  Neither of these worked.

 

Does anyone have a suggestion for the correct way to format these curly brace bound labels?

 



#2 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 25 November 2015 - 11:55 AM

Greetings Program!

 

Move the formatting to the laborCostAmt.cellActions.willDisplay action and set the text of the control there to the formatted value.

 

Wg


  • Laeger likes this

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#3 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 25 November 2015 - 12:36 PM

Wizgod,

Ok, it does indeed work if I implement the willDisplay cellAction for the propertyDef, but doesn't seem to work for the cellAction from the section, model, etc.  So thank you for that, I'll add that to the STV bag 'o tricks to try when things aren't working as expected.

 

Next question, since this particular cell has 2 labels I configured it as an SCCustomCell.  In order to test your method I grabbed the actual tag (assigned by STV) of the label I wanted to format and directly manipulated it that way.  Obviously this is a poor way to implement it, so is the better approach to subclass SCCustomCell and assign that as the class of the cell in IB?  I assume that then I could use the outlets I create to get the correct label.

 

What do you think?

(And thanks again!)



#4 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 25 November 2015 - 01:08 PM

I myself subclass as much as possible, not just for re-usability, but it makes it easier to deal with and you have greater control; it keeps the main view controller class smaller for sure.

 

Try it and see how well it works for you.

 

Wg


P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#5 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 01 December 2015 - 02:53 PM

Hey guys,

 

@laeger: I am not sure why your code isn't working, but have you tried curly brace binding in the following format: 

 

${laborCostAmt}.00


#6 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 04 December 2015 - 08:49 AM

The only way I could get it to format correctly was with WG's method.  Putting the formatting in anything other than the property def did not work.

 

Using your suggestion does indeed put a dollar sign in front and 2 decimals behind, but it doesn't add the thousands separator.  It also does not internationalize and change the currency symbol.

The only way I could find to get all of that to work is to use a number formatter, and call it in propertyDef.cellActions.willDisplay



#7 halldorg

halldorg

    Jr. Member

  • STV 5.0 Std
  • PipPip
  • 26 posts
Reputation: 5
Good

Posted 11 December 2015 - 06:03 PM

Is there anywhere proper and detailed documentation on curly brace binding and formatting options?

 

Does it support any date formatting?



#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 13 December 2015 - 06:56 AM

Hi halldorg,

 

Curly brace binding is introduced in this manual tutorial, and this video tutorial. It's really very simple, you just include your property's name between braces:

 

My task name is: {name}

My speed is: {speed}m/s

 

The only other thing you need to know is that you can also specify the number of decimal places STV should display for your number values. You specify this by placing a dot and a number of zeros representing the decimal places. For instance, to specify 3 decimal places:

 

Circle Radius: {radius}.000

 

 

Re date formatting: Dates are always formatted using the date formatter specified in its property definition. The property definition's date formatter can either be specified in code, or more conveniently, via IB as shown below:

 

Attached File  DateFormatter.png   180.55K   18 downloads



#9 halldorg

halldorg

    Jr. Member

  • STV 5.0 Std
  • PipPip
  • 26 posts
Reputation: 5
Good

Posted 13 December 2015 - 10:50 AM

Thanks Tarek,

I had seen the samples and it is very simple but it's a bit confusing that there is nothing about it anywhere in the documentation.

 

Because there is nothing there it is not obvious that the only formatting option is number of decimal places(and no way to get a thousand separator using this method) - features like this really need to be included in the documentation.

 

It's really a key feature that deserves more documentation.

 

Thanks again,

Halldor

 

Hi halldorg,

 

Curly brace binding is introduced in this manual tutorial, and this video tutorial. It's really very simple, you just include your property's name between braces:

 

My task name is: {name}

My speed is: {speed}m/s

 

The only other thing you need to know is that you can also specify the number of decimal places STV should display for your number values. You specify this by placing a dot and a number of zeros representing the decimal places. For instance, to specify 3 decimal places:

 

Circle Radius: {radius}.000

 

 

Re date formatting: Dates are always formatted using the date formatter specified in its property definition. The property definition's date formatter can either be specified in code, or more conveniently, via IB as shown below:

 

attachicon.gifDateFormatter.png


  • Laeger likes this

#10 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 14 December 2015 - 05:31 AM

Yes Halldor, you're absolutely right about this.



#11 Laeger

Laeger

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 59 posts
Reputation: 6
Good

Posted 14 December 2015 - 05:43 AM

I concur with halldorg!  I think this is a fantastic feature as well, and having the ability to do w/ numbers what we can do w/ dates would be great (e.g. having something similar to the date formatter text box that is in IB).


  • Tarek likes this

#12 Donovan Dillon

Donovan Dillon

    Experienced Member

  • STV 5.0 Std
  • PipPipPipPip
  • 76 posts
Reputation: 8
Good

Posted 10 June 2016 - 01:23 PM

Hi Tarek,

Reviving this thread to +1 halldorg and Laeger and the request for more curly brace numerical formatting options beyond just decimal places.

At a minimum, it would be great to have localized thousands separators and currency formatting as indicated in this thread.

Has either of these been added to STV?

Thanks!

Donovan





Also tagged with one or more of these keywords: cells

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users