UA-17470720-3

Jump to content


Photo
- - - - -

Using a dictionary field as an object selection key


  • Please log in to reply
2 replies to this topic

#1 Brian

Brian

    Starting Out

  • STV Lite
  • Pip
  • 6 posts
Reputation: 0
Neutral

Posted 20 May 2014 - 07:19 PM

I'm currently doing the following to replace a field's UI with a selection UI.

    NSArray *choices = @[@"Alabama", @"Alaska" /* ... */];
    SCEntityDefinition *entityDef = [SCEntityDefinition definitionWithEntityName:self.entityName
                                                            managedObjectContext:context
                                                                   propertyNames:self.propertyNames
                                                                  propertyTitles:self.propertyTitles];

    SCPropertyDefinition *stateDef = [entityDef propertyDefinitionWithName:@"State"];
    stateDef.type = SCPropertyTypeSelection;
    stateDef.attributes = [SCSelectionAttributes attributesWithItems:choices allowMultipleSelection:NO allowNoSelection:YES autoDismissDetailView:YES hideDetailViewNavigationBar:NO];

This works, but now I need to change it so that instead of saving the selected index in the underlying model, it saves one of the fields of a dictionary:

NSDictionary *choices = @[
                            @{@"ID": @1, @"Name": @"Alabama"},
                            @{@"ID": @2, @"Name": @"Alaska"},
                            /* ... */
                        ];

I want the UI to display the Name values in the list, but persist the ID values.

 

I believe SCPropertyTypeObjectSelection will apply the object instead of the index, but I'm not sure how to persuade it to use the ID from the dictionary. Is there a way?

 



#2 Brian

Brian

    Starting Out

  • STV Lite
  • Pip
  • 6 posts
Reputation: 0
Neutral

Posted 21 May 2014 - 08:14 AM

On a possibly related note, I'm currently attempting to solve this by using the didLoadBoundValue and willCommitBoundValue cell actions. The latter is doing what I want, but didLoadBoundValue does not seem to be getting called (my log message is not showing up, while the one in willCommitBoundValue does). What might prevent didLoadBoundValue from getting called?



#3 Brian

Brian

    Starting Out

  • STV Lite
  • Pip
  • 6 posts
Reputation: 0
Neutral

Posted 21 May 2014 - 09:37 AM

More self-responses!

 

This is the code I ended up with:

 

    stateDef.cellActions.didLoadBoundValue = ^NSObject*(SCTableViewCell *cell, NSIndexPath *indexPath, NSObject *value) {
        return [NSNumber numberWithInt:[choices indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
            NSNumber *choiceId = (NSNumber*)value;
            return [obj[@"ID"] isEqualToNumber:choiceId];
        }]];
    };
    stateDef.cellActions.willCommitBoundValue = ^NSObject*(SCTableViewCell *cell, NSIndexPath *indexPath, NSObject *value) {
        NSNumber *index = (NSNumber*)value;
        return choices[[index intValue]][@"ID"];
    };
 

 

This works, with the caveat that didLoadBoundValue does not get called in 3.3.0 when calling generateSectionsForObject. This appears to be because the cellActions are propogated to the cell in SCTableViewSection.m lines 1068-1069, but the bound value is read prior to that at line 902. The workaround is to call [tableViewModel reloadBoundValues] after generateSectionsForObject.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users