UA-17470720-3

Jump to content


Photo
- - - - -

STV4 Core Data - How to make a cell that is a calculated field

cells actions core-data

  • Please log in to reply
11 replies to this topic

#1 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 14 December 2014 - 09:40 PM

Hi, I want to have a cell (age) that is calculated when a record is added or changed. 

 

age is currentYear - birthYear (roughly)

 

I was thinking of something like this:

 

 



SCTextFieldCell *ageCell = (SCTextFieldCell *)[objectSection cellForPropertyName:@"age"]; (for changes)

SCTextFieldCell *yearCell = (SCTextFieldCell *)[objectSection cellForPropertyName:@"year"];

double year = [yearCell.textField.text doubleValue];

double age = year - birthYear;

ageCell.textField.text = [NSString stringWithFormat:@"%f", age];

}
 

 

Can someone point me in the right direction please...



#2 ozie

ozie

    ¯\_(ツ)_/¯

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

Posted 15 December 2014 - 12:00 AM

could use the valueChanged method for each cell

.cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)

 

then within each method get that cells value, and then get the value of the other cell.. something like

SCTextFieldCell *yearCell = (SCTextFieldCell*)[cell.ownerSection cellAtIndex:2];

 

do your maths and update the cell you want to show it in

 

i would probably in the valueChanged call its own method to make the code neat and both cells valueChange call that


  • David DelMonte likes this

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


#3 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 15 December 2014 - 10:19 AM

Thanks Ozie, that's very helpful. Where is a good place to add the cellActions? In STV, we had discrete detailViewControllers, now, do I still have to make it? (I can add it to the AddVC, but it may need to be edited).



#4 ozie

ozie

    ¯\_(ツ)_/¯

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

Posted 15 December 2014 - 04:06 PM

something like

    SCTextFieldCell *yearCell = (SCTextFieldCell *)[objectSection cellForPropertyName:@"year"];
    yearCell.cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        SCObjectSection *section = (SCObjectSection*)cell.ownerSection;
        double year = [((SCTextFieldCell*)cell).textField.text doubleValue];

        double age = nowYear - year;
        SCTextFieldCell *ageCell = (SCTextFieldCell *)[section cellForPropertyName:@"age"];
        ageCell.textField.text = [NSString stringWithFormat:@"%f", age];
    };

  • David DelMonte likes this

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


#5 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 16 December 2014 - 10:16 AM

@Tarek, it would be helpful to know whether I should make a discrete DetailViewController class, to use my calculated field. I expect I do, but here's a good place for another of your great tutorials.

 

David



#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 17 December 2014 - 09:27 AM

Hi David,

 

Are you creating the cells manually, or are they generated by a data definition? 



#7 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 17 December 2014 - 09:52 AM

Hi Tarek, they are from a data definition (property group)  listed  a core data graph. That is being picked up in IB.



#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 17 December 2014 - 10:08 AM

Then the best way would be configuring your cellsActions right from the data definition as follows:

 

    typeof(self) weak_self = self;
    SCPropertyDefinition *agePDef = [myDataDef propertyDefinitionWithName:@"age"];
    agePDef.cellActions.willDisplay = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        NSInteger year = [(NSNumber *)[cell.boundObject valueForKey:@"year"] integerValue];
        NSInteger age = [weak_self ageFromYear:year];
        
        // update age cell here as you see fit
    };
    SCPropertyDefinition *yearPDef = [myDataDef propertyDefinitionWithName:@"year"];
    yearPDef.cellActions.valueChanged = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        NSInteger year = [(NSNumber *)[cell.boundObject valueForKey:@"year"] integerValue];
        NSInteger age = [weak_self ageFromYear:year];
        
        SCObjectSection *objectSection = (SCObjectSection *)[cell ownerSection];
        SCTableViewCell *ageCell = [objectSection cellForPropertyName:@"age"];
        
        // update age cell here as you see fit
    };

 

Hope this helps.



#9 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 18 December 2014 - 02:51 PM

OK, I got it with this code in Master VC > ViewDidLoad:

 

 

SCArrayOfObjectsSection *objectsSection = (SCArrayOfObjectsSection *)[self.tableViewModel sectionAtIndex:0];
objectsSection.cellActions.willDisplay = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
{
NSInteger year = [(NSNumber *)[cell.boundObject valueForKey:@"year"] integerValue];
NSInteger age = year - birthYear;
NSString *ageString = [NSString stringWithFormat:@"%i", age ];
[cell.boundObject setValue:ageString forKey:@"age"];

};
 


#10 ozie

ozie

    ¯\_(ツ)_/¯

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

Posted 18 December 2014 - 03:01 PM

I'm not sure why you are doing it like this:) seems that the age value only updates if the cell is ever visible.. is this the point?

if i was to print the data in 5 years their age would be as it was 5 yrs ago.. but would you want the data to print with its age on the day it was printed?

 

if its the latter i think the better way would be to add a function inside your coredata managed object class that calc's the age.. you would still use the willDisplay to display the cells age value, but use the objects function to get the age


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


#11 David DelMonte

David DelMonte

    Forum Master

  • STV 4.0 Pro
  • PipPipPipPipPipPipPip
  • 527 posts
Reputation: 41
Excellent

Posted 18 December 2014 - 03:05 PM

 Ozie, the age is when something happened, not the age when the report is printed/viewed. Still, it may be better to move this function to the Managed Object Class. Thanks for pointing that out.



#12 ozie

ozie

    ¯\_(ツ)_/¯

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

Posted 18 December 2014 - 03:07 PM

yes function would make life easy:)


Edited by ozie, 18 December 2014 - 03:09 PM.

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






Also tagged with one or more of these keywords: cells, actions, core-data

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users