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
}];
7 Answers, 1 is accepted
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
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);
}];
}];
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;
}
}
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
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;
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