UA-17470720-3

Jump to content


Photo
- - - - -

How to create a hierarchical selection list from a dictionary of NSArrays of NSStrings?


  • Please log in to reply
4 replies to this topic

#1 notreallyme

notreallyme

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 82 posts
Reputation: 8
Good

Posted 24 October 2013 - 07:05 AM

Hello,

 

i read the documentation and was not succesful to adopt the examples to the following task. What i need is easy to describe, but seems impossible to implement with STV:

 

I have a dictionary of NSArrays, which represent a hierarchical list of names (here contract names). The first level of the dictionary contains contract types (which i want to present in an alphabetical order) and the second level contains arrays of strings, of which globally at max one should have a selection mark at it. The purpose of this two level selection is to let the user select one contract (by tipping on it), but not to confront him with the complete (huge) list of contracts at the first level. How can i automatize this task with a few lines of code in STV?

 

Best regards, Helmut.


Edited by Helmut Neumann, 24 October 2013 - 07:34 AM.


#2 Aris Kox

Aris Kox

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 35 posts
Reputation: 14
Good

Posted 24 October 2013 - 09:34 AM

If you transform the first level dictionary into an array of dictionaries, you could do something like this

 

- (void)viewDidLoad

{

    [super viewDidLoad];

 

// setup Data

 

    NSArray *arr1 = @[ @"option1", @"option2", @"option3"];

    NSArray *arr2 = @[ @"option11", @"option12", @"option13"];

    NSArray *arr3 = @[ @"option21", @"option22", @"option23"];

    NSArray *arr4 = @[ @"option31", @"option32", @"option33"];

 

    NSMutableArray *firstLevel = [ @[ @{@"title":@"dict1", @"moreitems":arr1},

                                  @{@"title":@"dict2", @"moreitems":arr2},

                                  @{@"title":@"dict3", @"moreitems":arr3},

                                  @{@"title":@"dict4", @"moreitems":arr4}] mutableCopy];

    

// Setup definitions and sections

 

    SCDictionaryDefinition *def = [SCDictionaryDefinition definitionWithDictionaryKeyNamesString:@"moreitems"];

    

    def.titlePropertyName = @"title";

    

    SCPropertyDefinition *valType = [def propertyDefinitionWithName:@"moreitems"];

    valType.type = SCPropertyTypeObjectSelection;

    SCClassDefinition *def2 = [SCClassDefinition definitionWithClass:[NSString class] propertyNames:nil];

    

    valType.attributes = [SCObjectSelectionAttributes attributesWithSelectionObjects:nil objectsDefinition:def2 allowMultipleSelection:NO allowNoSelection:NO];

    

    valType.attributes.expandContentInCurrentView = YES;

    

    __weak typeof(self) weakSelf = self;

 

    valType.cellActions.didSelect = ^(SCTableViewCell *aCell, NSIndexPath *indexPath)

    {

        // item selected use 'weakSelf' to execute an action

    };

 

    

    SCArrayOfObjectsSection *section = [SCArrayOfObjectsSection sectionWithHeaderTitle:nil items:firstLevel itemsDefinition:def];

    section.allowAddingItems = NO;

    section.allowDeletingItems = NO;

    section.allowMovingItems = NO;

    

    [self.tableViewModel addSection:section];

    

    section.sectionActions.detailModelConfigured =^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath)

    {

        // Set the new items on the selection section

        

        NSDictionary *boundDict = (NSDictionary *) [section cellAtIndex:indexPath.row].boundObject;

        

        SCObjectSelectionSection *s = (SCObjectSelectionSection *) [detailModel sectionAtIndex:0];

 

        [s.mutableItems setArray:[boundDict objectForKey:@"moreitems"]];

    };

 

}



#3 notreallyme

notreallyme

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 82 posts
Reputation: 8
Good

Posted 24 October 2013 - 11:42 PM

Thank you for the proposal. The structure is in fact already a dictionary of dictionaries, but a completely dynamic one, i can not hard code the members of the dictionary.



#4 Aris Kox

Aris Kox

    Sr. Member

  • STV 5.0 Pro
  • PipPipPip
  • 35 posts
Reputation: 14
Good

Posted 25 October 2013 - 03:18 AM

You don't have to hardcode anything, except the key names.

 

There is an easier solution though. Create two SCViewController, one for each level. Both should have a SCSelectionSection which contain your list values



#5 notreallyme

notreallyme

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 82 posts
Reputation: 8
Good

Posted 25 October 2013 - 04:32 AM

It was too complicated to be done with STV. Instead i did it now the old fashioned way in just a few minutes :-).






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users