UA-17470720-3

Jump to content


Photo
- - - - -

Is SCPropertyTypeObjectSelection not the right choice?

core-data cells

  • Please log in to reply
5 replies to this topic

#1 Laeger

Laeger

    Experienced Member

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

Posted 08 January 2014 - 02:45 PM

Hello,

I am using CoreData, and trying to model a to-many relationship.  I have an entityDef for a Facility which has a to-many relationship to Users.

I present a list of all Facilities, and when one is selected a detail view shows displaying the selected facility.  So far so good.

Now one of the properties that I want to be able to display is which User's are assigned to this facility. 

Here is what I have:

   SCEntityDefinition *entityDef = ... // this is the Facility definition 

    SCEntityDefinition *usersEntityDef = [User STV_entityDefinitionInManagedObjectContext:context];
    SCPropertyDefinition *usersPropertyDef = [entityDef propertyDefinitionWithName:@"users"];
    usersPropertyDef.type = SCPropertyTypeObjectSelection;
    SCObjectSelectionAttributes *usersAttributes = [SCObjectSelectionAttributes attributesWithObjectsEntityDefinition:usersEntityDef usingPredicate:nil allowMultipleSelection:YES allowNoSelection:YES];
    usersPropertyDef.attributes = usersAttributes;
    usersPropertyDef.cellActions.willConfigure = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        if ([cell isKindOfClass:[SCObjectSelectionCell class]]) {
            [(SCObjectSelectionCell*)cell setDisplaySelection:FALSE];
        }
    };
    usersPropertyDef.cellActions.willDisplay = ^(SCTableViewCell *cell, NSIndexPath *indexPath)
    {
        Facility *facility = (Facility *)cell.boundObject;
        assert([facility isKindOfClass:[Facility class]]);
        SCObjectSelectionCell *usersCell = (SCObjectSelectionCell *)cell;
        usersCell.label.text = [NSString stringWithFormat:@" %d user%@", facility.users.count, facility.users.count == 1 ? @"" : @"s"];
    };

What does work:

-Since there can be many users assigned to this facility, I do not want them displayed in the cell (and then truncated...).  setDisplaySelection:FALSE accomplishes this.

 

What DOESN'T work:

-When the detail view of the selected Facility is first presented, the label correctly displays the number of users related to this facility.  However, if I tap the cell and add or remove users, the label never changes.  The weird thing is that when I break in the cellActions.willDisplay block (which IS called after the selections are made and the list of users is dismissed), the boundObject has not changed!  BUT, if I save the context and come back, my changes are now reflected.

Should I be using a different property type and/or method to accomplish this?

 

Hopefully that makes sense, and thanks for any pointers!
-Aaron


Edited by Laeger, 08 January 2014 - 02:46 PM.


#2 ozie

ozie

    ¯\_(ツ)_/¯

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

Posted 08 January 2014 - 06:29 PM

im not sure if there is a better way, but i needed this once too and the only way i got it to work was with this code

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self cellCount];
}

- (void)cellCount {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@“Entity” inManagedObjectContext:moContext];
    [fetchRequest setEntity:entity];
    
    [[[self.tableViewModel sectionAtIndex:0] cellAtIndex:1] badgeView].text = [NSString stringWithFormat:@"%lu",(unsigned long)[moContext countForFetchRequest:fetchRequest error:nil]];
    
    fetchRequest = nil;
}

here i set the badge for the cell to indicate the number of items selected.. but it hard codes the cell that needs changing

if there is a better way i too would like to know:)


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


#3 ozie

ozie

    ¯\_(ツ)_/¯

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

Posted 08 January 2014 - 09:27 PM

had another thought.. what about

change this
SCObjectSelectionCell *usersCell = (SCObjectSelectionCell *)cell;
        usersCell.label.text = [NSString stringWithFormat:@" %d user%@", facility.users.count, facility.users.count == 1 ? @"" : @"s"];

to 
SCObjectSelectionCell *usersCell = (SCObjectSelectionCell *)cell;
        usersCell.label.text = [NSString stringWithFormat:@" %d user%@", usersCell.selectedItemsIndexes.count, usersCell.selectedItemsIndexes.count == 1 ? @"" : @"s"];

that way you get the number of selected objects in the cells list


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


#4 Laeger

Laeger

    Experienced Member

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

Posted 09 January 2014 - 05:02 AM

Shazam!  Thanks ozie, that did the trick!

-Aaron



#5 ozie

ozie

    ¯\_(ツ)_/¯

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

Posted 09 January 2014 - 05:06 AM

which one? :)


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


#6 Laeger

Laeger

    Experienced Member

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

Posted 09 January 2014 - 06:40 AM

lol, both!

The second post got me on the right track for how to get the correct number of selections, and the first post reminded me of badgeView.  I have updated my code to use badgeView, but I did run into a problem with it:

http://sensiblecocoa...estion/?p=10225

Thanks again,

Aaron







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

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users