Search code examples
objective-cquickdialog

QDynamicDataSection in QuickDialog


Currently, I struggle with the QDynamicDataSection of QuickDialog. I want to populate a dynamic section. When I run the following code, taken from the demo, I get a dynamic section populated with several instances of the template, i.e. "Something here". However, I want to get "first" and "second". How can this be reached?

QDynamicDataSection *section = [QDynamicDataSection new];
section.title = @"Normal: with elements";
section.bind = @"iterate:something";
section.elementTemplate = [NSDictionary dictionaryWithObjectsAndKeys:
    @"QLabelElement", @"type",
    @"Something here", @"title",
nil];
[root addSection: section];
[root bindToObject:[NSDictionary dictionaryWithObjectsAndKeys:
        [NSArray arrayWithObjects:@"first", @"second", nil], @"something",
        nil]];

EDIT with solution:

Eduardo set me on the right track (and gets the credit):

  1. The template has to provide a binding
  2. The array elements to iterate over have to be key compilant, e.g., dictionaries. (bind to object does not seem to work)

The following code works as intended:

QDynamicDataSection *section = [QDynamicDataSection new];
section.title = @"Normal: with elements";
section.bind = @"iterate:something";
section.elementTemplate = [NSDictionary dictionaryWithObjectsAndKeys:
    @"QLabelElement", @"type",
    @"title:name", @"bind",
nil];
[root addSection: section];

[root bindToObject:[NSDictionary dictionaryWithObjectsAndKeys:
        [NSArray arrayWithObjects:
         [NSDictionary dictionaryWithObject: @"first" forKey: @"name"],
         [NSDictionary dictionaryWithObject: @"second" forKey: @"name"], 
         nil], @"something",
        nil]];

return root; 

@Eduardo Scoz: I would suggest to adopt the exampe code in SampleDataBuilder.m.


2nd Edit The approach with self (see comment by Eduardo) works, too.


Solution

  • The "iterate" key simply iterates over the items in your dictionary, and creates a new element based on the template for each element. It also binds the new item to the object in the array.

    In your case though, your template doesn't define any binding, so the new item created doesn't have any value modified. You'll have to add something like @"bind", @"title:object". (object returns the actual object being bound, so you're not binding against a property of it.