1

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.

Matthias
  • 8,018
  • 2
  • 27
  • 53

1 Answers1

1

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.

Eduardo Scoz
  • 24,653
  • 6
  • 47
  • 62
  • Thanks for the answer. However, adding `@"bind", @"title:object"` to the template directory simply leads to an exception (class is not key value coding-compliant for the key title:object). In addition, the code is an original copy from the example you've provided (file: SampleDataBuilder.m, lines 636 ff), thus I guess, I don't understand the intended semantics of the example. – Matthias Jun 19 '12 at 06:01
  • My mistake, it should be the opposite: @"title:object", @"bind", (just below the QLabelElement line). I hate that NSDictionary uses value->key, instead of key->value. – Eduardo Scoz Jun 19 '12 at 13:10
  • Thanks again. I did already guess, that the parameters have to be exchanged. However, in this case (again in your example code) I get the very same exception again, this time for `NSCFConstantString` (in the first case for `QLabelElement`). The execption is raised in the `bindToObject` call. Is there something wrong, maybe? – Matthias Jun 19 '12 at 14:28
  • Sorry I keep guessing without really checking. It should be "title:self". "Self" returns the current object being bound to the root, not "object". Give that a try. Code is here: https://github.com/escoz/QuickDialog/blob/master/quickdialog/QBindingEvaluator.m – Eduardo Scoz Jun 19 '12 at 15:02