DataForm valuesProvider

8 posts, 0 answers
  1. Ryan
    Ryan avatar
    25 posts
    Member since:
    Jun 2016

    Posted 01 Jun Link to this post

    I need to get some items from my database in order to display them in a TKDataFormPickerViewEditor. My understanding is that I have to set the valuesProvider property and then if I want to select a specific one I would have to set the valueCandidate property. I can display all of the items correctly but I cannot get it to select a specific one.

     

    _dataSource[@"description"].editorClass = [TKDataFormPickerViewEditor class];

    _dataSource[@"description"].valuesProvider = @[self.description];

    _dataSource[@"description"].valueCandidate = self.description;

     

    [self getDescriptions:parameters{

            //this method may be called on a thread other than the main thread and occurs on successfully getting the data back from the database

            NSArray *descriptions = ...;        //gotten from database;
            NSInteger index = [descriptions indexOfObject:self.description];
            _dataSource[@"description"].valuesProvider = descriptions;
            [_dataForm reloadData];    //this reloads the form so that it displays all of the descriptions

            _dataSource[@"description"].valueCandidate = self.description;    //this isn't selecting the one I want
    }]; 

  2. Adrian
    Admin
    Adrian avatar
    167 posts

    Posted 06 Jun Link to this post

    Hi, Ryan,

    Thank you for writing.
    The correct way to achieve this is to use the selectedIndex property of TKDataFormPickerViewEditor. You should adopt TKDataFormDelegate and implement its dataForm:setupEditor:forProperty:. Consider the code below:
    - (void)dataForm:(TKDataForm *)dataForm setupEditor:(TKDataFormEditor *)editor forProperty:(TKEntityProperty *)property {
        if ([editor isKindOfClass:TKDataFormPickerViewEditor.class]) {
            ((TKDataFormPickerViewEditor *)editor).selectedIndex = 2;
            UIPickerView *pickerView = ((UIPickerView *)editor.editor);
            [pickerView selectRow:2 inComponent:0 animated:NO];
        }
    }

    I hope this helps.

    Regards,
    Adrian
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Ryan
    Ryan avatar
    25 posts
    Member since:
    Jun 2016

    Posted 13 Jun in reply to Adrian Link to this post

    This delegate method gets called when the screen first appears. How can I call this after that? Since I am getting data from a database it could take a while to get all of the data that I need. After I get all of my data is when I need to select the correct one.
  5. Ryan
    Ryan avatar
    25 posts
    Member since:
    Jun 2016

    Posted 13 Jun in reply to Ryan Link to this post

    Also how would I do this for TKDataFormOptionsEditor? After calling reloadData inside this delegate method it shows all of the correct options but when I set the selectedIndex property of the TKDataFormOptionsEditor, it crashes saying that it is out of bounds.
  6. Ryan
    Ryan avatar
    25 posts
    Member since:
    Jun 2016

    Posted 13 Jun in reply to Ryan Link to this post

    For example if I am doing something like this it does not work and crashes on the line where I set the valueCandidate:

     

    [[NSOperationQueue new]  addOperationWithBlock:^{

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                _dataSource[@"desc"].valuesProvider = @[@"test1", @"test2" ];
                _dataSource[@"desc"].valueCandidate = @(1);
            }];

        }];

  7. Sophi
    Admin
    Sophi avatar
    99 posts

    Posted 15 Jun Link to this post

    Hello Ryan,

    In order to preselect value in TKDataFormOptionsEditor you can apply the same login in the setupEditor method.
    - (void)dataForm:(TKDataForm *)dataForm setupEditor:(TKDataFormEditor *)editor forProperty:(TKEntityProperty *)property {
        if ([editor isKindOfClass:TKDataFormOptionsEditor.class]) {
            ((TKDataFormOptionsEditor *)editor).selectedIndex = 2;
        }
    }
    Consider the snippet above.

    Regarding the remote data loading, you can't change the initial invocation moment of the delegate methods. However you can call the reloadData method after your data is loaded or perform synchronizing logic in order to display your data properly.

    Regards,
    Sophi
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  8. Ryan
    Ryan avatar
    25 posts
    Member since:
    Jun 2016

    Posted 15 Jun in reply to Sophi Link to this post

    When setting the selectedIndex inside the setupeditor method after setting the valuesCandidate property and calling reloadData:

    *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 0]'

     

    When I examine the options array below it displays more than one option in the debugger:

    NSArray *options = ((TKDataFormOptionsEditor*) editor).options;

  9. Sophi
    Admin
    Sophi avatar
    99 posts

    Posted 20 Jun Link to this post

    Hello Ryan,

    When you are using the setupEditor method to preselect an item you don't need to use the valueCandidate property. 
    Could you, please, send us a code snippet of your TKDataForm setup or maybe a simple example project where we can actually observe the behavior you are experiencing, this way we will be able to examine your scenario and provide you with solution in short terms. 

    Thank you for your understanding.
    Looking forward to hearing from you.

    Regards,
    Sophi
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner