UA-17470720-3

Jump to content


Photo
- - - - -

Core Data Template - Access details properties


  • Please log in to reply
35 replies to this topic

#1 yesss

yesss

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 12 posts
Reputation: 2
Good

Posted 14 August 2015 - 01:48 AM

Hi,

I have created an app using the Core Data application template in STV 5.0 following the Task app tutorial.

 

I found it very powerful and in no time I have been able to create an app that is working perfectly, in witch I can create Locations (same as Tasks), set each property (i.e. name, etc...) and everything is saved and managed with Core Data.

I managed to customise the UI both for the Locations List and for the Location Details, which is an auto generated UI.

 

What I need now is to be able to change some of properties values in the Location Details screen calculating them using the values in other fields.

e.g. the value of a property called TotalCost should be calculated using the values of the PricePerUnit and the NumberOfUnits properties.

 

The MasterViewController is empty and because the Details screen is auto generated there is no DetailsViewController...

 

How can I access the properties and relative objects (textFields, labels, etc...) and where should I do it?

 

In a "non-STV environment" I would have used properties and outlets, both in a MasterViewController and in a DetailsViewController, but using STV visually and with auto generated UI I am now a bit lost...

 

I would be grateful if you could help me with this issue

 

Thank you very much



#2 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 14 August 2015 - 02:38 AM

Hi yesss

 

there are numerous way to achieve what your trying to do

- you could create a custom cell and access other cells values in the view to use in calculations

- you can create your own code file and link this to the IB view you want

- in code of the main view, you can link to the tableviewmodel.detailviewcontroller and use action events to get and set cell values

 

what sort of example are you looking for.. and to make it even easier what code are you using?? objC or swift??

 

oz


  • Tarek and yesss like this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#3 yesss

yesss

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 12 posts
Reputation: 2
Good

Posted 14 August 2015 - 05:14 AM

Hi Oz,

thank you very much for your reply, much appreciated.

 

For you to see exactly what I am trying to achieve I have taken 2 screenshots:

 

1) Locations List

https://www.dropbox...._list.tiff?dl=0

 

This is the main screen with the list of all the locations

 

2) Details Screen

https://www.dropbox....creen.tiff?dl=0

 

This is the screen you see when clicking on a location. It is an auto generated UI and each field correspond to an attribute of the LocationEntity.

As you can see in the screenshots, several attributes' values must be calculated using the values of other attributes (I put letters next to each field and formulas next to the fields which must be calculated, hope this is clear enough...)

 

I am using objC code

 

Thank you very much for your time and help, I really appreciate it.



#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 14 August 2015 - 07:17 AM

Hey yesss,

 

As ozie suggested, one of the easiest methods to achieve this is by using cell actions. Here is some code to help get you started (based on our CoreDataApp sample):

 

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // fetch the task entity definition created in IB
    SCDataDefinition *taskEntityDef = [self dataDefinitionWithIBName:@"TaskEntity"];
    
    // now let's assume we've added a new calcualted property in IB called 'daysUntilDue' of type 'Label'
    SCPropertyDefinition *daysUntilDuePDef = [taskEntityDef propertyDefinitionWithName:@"daysUntilDue"];
    
    // 1st, let's calculate daysUntilDue whenever its cell is displayed
    typeof(self) weak_self = self;  // create weak_self to avoid cyclic refrencing inside blocks
    daysUntilDuePDef.cellActions.willDisplay = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        NSDate *taskDueDate = (NSDate *)[cell.boundObject valueForKey:@"dueDate"];  // cell.boundObject is the 'Task' object
        
        SCLabelCell *daysUntilDueCell = (SCLabelCell *)cell;
        daysUntilDueCell.label.text = [weak_self stringNumberOfDaysSince:taskDueDate];
    };
    
    // now let's recalculate whenever the user changes the task's dueDate
    SCPropertyDefinition *dueDatePDef = [taskEntityDef propertyDefinitionWithName:@"dueDate"];
    dueDatePDef.cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        NSDate *taskDueDate = (NSDate *)[cell.boundObject valueForKey:@"dueDate"];
        
        // get daysUntilDueCell
        SCObjectSection *objectSection = (SCObjectSection *)[cell ownerSection];
        SCLabelCell *daysUntilDueCell = (SCLabelCell *)[objectSection cellForPropertyName:@"daysUntilDue"];
        
        daysUntilDueCell.label.text = [weak_self stringNumberOfDaysSince:taskDueDate];
    };
}

- (NSString *)stringNumberOfDaysSince:(NSDate *)date
{
    NSTimeInterval numberOfSeconds = [date timeIntervalSinceNow];
    NSInteger numberOfDays = numberOfSeconds / 86400;
    
    return [NSString stringWithFormat:@"%li", (long)numberOfDays];
}

@end

 

Please let me know if you have any issues or questions with the above code. Good luck!


Edited by Tarek, 14 August 2015 - 07:25 AM.

  • yesss and wizgod like this

#5 yesss

yesss

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 12 posts
Reputation: 2
Good

Posted 14 August 2015 - 01:05 PM

Hi Tarek,
thank you very much for your reply, it's been very helpful!
 
I followed your advice and used the example code to calculate the values of the properties I wanted.
 
I have however still 3 issues:
 
1st issue:
the values are calculated only when I click on the "Done" button, not when I change the others fields values.
 
What I would like is to see the auto-calculated fields' values change when I finish editing one of the other textfields, but now I have to click 'Done' which takes me out of the Details screen, and then enter the Details screen again...
 
Before bothering you again I tried to dig in the forum and I found another post where you suggested the user to:
 
a- set the delegate to the detail model using this method:
 
- (void)tableViewModel:(SCTableViewModel *)tableViewModel detailModelCreatedForRowAtIndexPath:(NSIndexPath *)indexPath
detailTableViewModel:(SCTableViewModel *)detailTableViewModel {
    detailTableViewModel.delegate = self;
    
    // Set the tag property of the detail model to correspond to its level in the hierarchy
    detailTableViewModel.tag = tableViewModel.tag + 1;
}
 
I tried it in my app but I get an error saying: "property delegate not found for object of type SCTableViewModel"
 
 
b- The the cells' boundValue only changes when the end-user actually taps the 'Done' button and the changes get committed to Core Data. To access the cells' values before they're committed, simply:
 
- (void)tableViewModel:(SCTableViewModel *) tableViewModel valueChangedForRowAtIndexPath:(NSIndexPath *) indexPath
{
SCTableViewSection *section = [tableViewModel sectionAtIndex:indexPath.section];
if([section isKindOfClass:[SCObjectSection class]])
{
    SCObjectSection *objectSection = (SCObjectSection *)section;
    
    // Now assuming you have a property called "income" in the class def
    SCTextFieldCell *incomeCell = (SCTextFieldCell *)[objectSection cellForPropertyName:@"income"];
    double incomeValue = [incomeCell.textField.text doubleValue];
 
    // etc...
    ...
}
}
 
this method doesn't get fired I assume because I haven't managed to set the delegate for the detail model?
 
 
2nd issue:
Curly Brace Binding not working anymore
 
In the Locations List I have 2 fields where I use {nameOfTheProperty1} and {nameOfTheProperty2} to get the corresponding values.
It worked fine before I changed Property1 and Property2 from textfields to labels and used your code to auto calculate their values.

 

How can I use curly brace binding to get the values of the auto calculated properties displayed into the general list cell?

 

 

3rd issue:

Generate Detail Section breaks auto calcultation

 
If I leave the UI as generated from the template the auto calculated fields work fine.
If in the storyboard I click on "Generate Details Section(s)" button to customise the details screen's UI, the details screen is correctly generated in the storyboard but the auto calculated field are no more automatically calculated.
Whatever I enter in the other fields, clicking "done" button etc. they don't change.
 
 
Sorry to bother you again, I tried to sort those 3 issues myself but didn't manage to do so...
 
I hope this is clear enough,
thank you for your time and help, much appreciated

Edited by yesss, 14 August 2015 - 02:44 PM.


#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 14 August 2015 - 02:45 PM

Hi yesss,

 

You're most welcome, it's always my pleasure!

 

The code you just posted uses STV delegates, which have been deprecated since STV 3.0 (hence the compiler errors). We now use the much simpler and more modern STV Actions, which my code above utilizes. 

 

 

Now to your issues: I am sorry I wasn't aware that you were using curly brace binding. I was just going to suggest some additional code updates to accommodate that, but then realized that my code is getting too complicated and is not in harmony with the overall simplicity of STV. If this isn't an urgent project for you, we can roll out a new STV update in about 10 days from now that will make implementing calculated cells a lot more simpler, and will automatically take care of updating these cells when other cells that they depend on change. Please let me know if you can wait that long.

 

Thank you!


  • Dave Guerin, yesss and ozie like this

#7 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 14 August 2015 - 02:46 PM

the key to your problem is controlling the cell's valueChanged action

// Objective-C
cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
{
    NSLog(@"Cell at indexPath:%@ value has changed to: %@.", indexPath, cell.boundValue);
};

delegates are very rarely used in STV anymore.. actions control 99% of everything now

using Tareks code sample above.. calculating (and referencing your images) "H" cell would work nice

dueDatePDef.cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        NSDate *taskDueDate = (NSDate *)[cell.boundObject valueForKey:@"dueDate"];
        
        // get daysUntilDueCell
        SCObjectSection *objectSection = (SCObjectSection *)[cell ownerSection];
        SCLabelCell *daysUntilDueCell = (SCLabelCell *)[objectSection cellForPropertyName:@"daysUntilDue"];
        
        daysUntilDueCell.label.text = [weak_self stringNumberOfDaysSince:taskDueDate];
    };

since cells A,B,H are all in the same section.. the code line [cell ownerSection] would only access the section section in the tableview

 

For cells I,J,K etc this code would be a little different as you need to access the other sections to change their values.. and then update the tableview to display the new cells values you set


  • yesss likes this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#8 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 14 August 2015 - 02:48 PM

lol.. was typing away then this was added:)

 

 a new STV update in about 10 days from now that will make implementing calculated cells a lot more simpler, and will automatically take care of updating these cells when other cells that they depend on change. 

 

this sounds awesome ! 


  • Tarek likes this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#9 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 14 August 2015 - 02:50 PM

Thanks ozie!



#10 yesss

yesss

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 12 posts
Reputation: 2
Good

Posted 14 August 2015 - 02:55 PM

Hi Tarek and Oz,

thank you both for the time you dedicate in helping me with these issues.

 

@Tarek

I can definitely wait 10 days for the new release, sounds definitely awesome!

Only problem now will be resisting for 10 days to get it to play with it ;-)

 

Thank you guys, really impressed by the quality of the product and even more by the quality of the support!


Edited by yesss, 14 August 2015 - 02:55 PM.

  • Tarek likes this

#11 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 14 August 2015 - 03:00 PM

Awesome, I'll keep you guys posted.


  • yesss and wizgod like this

#12 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 15 August 2015 - 04:06 PM

can you talk about what sort of "calculations" we will be able to do with cells?? numbers only, or dates, or selected items count ??


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#13 yesss

yesss

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 12 posts
Reputation: 2
Good

Posted 16 August 2015 - 12:22 AM

In this specific app it's just numbers calculations, but I guess that the feature of implementing calculated cells more easily will be useful for many other type of apps, including calculations with dates, selected items counts etc.

I have another app which include CRM functionalities in which I use those types of calculations.
I developed it without STV but I am now seriously thinking of including STV in the project...

#14 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 17 August 2015 - 08:54 AM

Hey guys,

 

You'll be able to provide calculated cell values via two methods:

 

1. Via a new cell action called 'calculatedValue'. This is the quick and easy method if you don't want to use method #2.

 

2. Via your model itself by implementing a getter method. For example, in our CoreDataApp example from above, you would create an NSManagedObject subclass called 'TaskEntity', and then create a new property called 'daysUntilDue' and it's '-(NSInteger)daysUntilDue' getter method. In general, this is a much better design since all your calculations are done in the model itself.

 

Of course we're wide open to any other ideas you guys might have on providing calculated cell values.

 

P.S.: You could already use method #2 with the existing STV, but you'd still have the problem of the calculated cell values not being automatically updated as other cell values change. 


  • yesss, ozie and wizgod like this

#15 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 18 August 2015 - 01:02 AM

Mmm, interesting!

 

Will the new cellAction.calculatedValue also update the (example) daysUntilDue Core Data entity attribute automatically?

 

And will it be calculatedValue singular, or will there be more than one attribute that can be calculated?

 

I'm just curious as to how this might work.


Cheers,

Dave

www.dgapps.ie

#16 wizgod

wizgod

    I'm what you guys call a User

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

Posted 18 August 2015 - 01:03 AM

Greetings Programs!

I use #2 to calculate the age, once the user has selected the birthdate using the date picker, and manually update the cell.

In my .NET Business Forms Framework, I use an expressions evaluator library to evaluate dynamic expressions on any page controls to change the value of another control.

I use curly braces for getting values from other controls (which I regex) and I can also reference any public properties or methods in any class I make accessible to the evaluator. Once I do the regex on the curly braces with their values, the expression is evaluated and result returned.

"{kmEnd} - {kmStart} * {ratePerKm}"

In STV, maybe for simple math expressions like above we could have it in the storyboard and use NSExpression?

Wg
  • Tarek likes this

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


#17 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 18 August 2015 - 01:34 AM

im just wondering how an action would work

if i have cells A,B and C which C is A+B... i have the action of valueChanged for A and B, but how would and action on cell C know when A or B changed??


  • yesss likes this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#18 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 18 August 2015 - 04:27 PM

@dave: In this case, 'daysUntilDue' is not an actual attribute that is saved to disk, but is rather a calculated property. Are there any cases where you guys actually want to save these calculated fields to disk?

 

@Wg: That's another excellent suggestion! My only concern here is that things can get/look messy on the 'view' side with all these equations. I also usually prefer calculations to be on the 'model' side of my objects, not the 'view'. Having said that, there are times were the calculations are strictly view related (which is why we also added the new 'calculatedValue' cellAction). What do you guys think?

 

@ozie: The initial idea is to have a new SCTableViewCell property called something like: '@property (strong) NSSet *propertyNamesAffectingValue', and it will be accessible from IB for both property definitions and cells. If we end up implementing Wg's suggestion, then STV could also automatically fill in this property based on the values provided in the curly brace equation. 



#19 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 18 August 2015 - 04:30 PM

but how will cell C know that cell A's value changed and needs to update the view with what ever the equation is?


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#20 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 18 August 2015 - 04:46 PM

That's STV's job, STV will monitor the values and update cells as necessary.


  • Dave Guerin and wizgod like this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users