XLForm - How to hide a row depending on the value of another row?

316 views Asked by At

I could need some help.

I'm using XLForm in my Objective-C project. I have a first row with a picker where you have four different options. Then there is a second picker. But the second picker should only be available if a certain value is selected in the first picker.

I know there is a description on the github-site but I don't really understand what to do. So please help me doing this.

Here are the relevant parts of my code:

row = [XLFormRowDescriptor formRowDescriptorWithTag:@"repeat" rowType:XLFormRowDescriptorTypeSelectorPickerViewInline title:@"Wiederholen:" ];
row.cellClass = [LehrerXLFormInlineSelectorCell class];

NSMutableArray *selectionArray = [NSMutableArray array];

for (NSNumber *item in [APIDataReplacement appointmentRepeatingList]) {
    NSString *title = [APIDataReplacement appointmentRepeatingToString:item.intValue];
    [selectionArray addObject:[XLFormOptionsObject formOptionsObjectWithValue:item displayText:title]];
}

row.selectorOptions = selectionArray;
if ([selectionArray count] == 0) {
    [row setDisabled:@YES];
}

if (aItem) {

    int repeatingID = [[aItem appointmentRepeatingId] intValue];
    NSString *title = [APIDataReplacement appointmentRepeatingToString:repeatingID];
    row.value = [XLFormOptionsObject formOptionsObjectWithValue:[NSNumber numberWithInt:repeatingID] displayText:title];

}else{

    NSNumber *first = [[APIDataReplacement appointmentRepeatingList] firstObject];
    NSString *title = [APIDataReplacement appointmentRepeatingToString:first.intValue];
    row.value = [XLFormOptionsObject formOptionsObjectWithValue:first displayText:title];
}

[section addFormRow:row];


section = [XLFormSectionDescriptor formSectionWithTitle:@""];
[form addFormSection:section];


row = [XLFormRowDescriptor formRowDescriptorWithTag:@"weekday" rowType:XLFormRowDescriptorTypeSelectorPickerViewInline title: @"Wochentag:"];
row.cellClass = [LehrerXLFormInlineSelectorCell class];

NSMutableArray *selectionArray1 = [NSMutableArray array];

for (NSNumber *item in [APIDataReplacement dayList]) {
    NSString *title = [APIDataReplacement dayToString:item.intValue];
    [selectionArray1 addObject:[XLFormOptionsObject formOptionsObjectWithValue:item displayText:title]];
}

row.selectorOptions = selectionArray1;

if (aItem) {

    // AppointmentRepeating *currentRepeat = [aItem appointmentRepeating];
    int dayID = [[aItem startday] intValue];
    NSString *title = [APIDataReplacement dayToString:dayID];
    row.value = [XLFormOptionsObject formOptionsObjectWithValue:[NSNumber numberWithInt:dayID] displayText:title];

}else{

    NSNumber *first = [[APIDataReplacement dayList] firstObject];
    NSString *title = [APIDataReplacement dayToString:first.intValue];
    row.value = [XLFormOptionsObject formOptionsObjectWithValue:first displayText:title];
}

[section addFormRow:row];


section = [XLFormSectionDescriptor formSectionWithTitle:@""];
[form addFormSection:section];

These are the two pickers. I think I should do something in the formRowDescriptorValueHasChanged-method, but I don't know what. If the first picker has the value "20" then the second picker should be hidden.

Really appreciate your help.

1

There are 1 answers

0
Aris On

For your second XLFormRowDescriptor you should:

secondRow.hidden = [NSString stringWithFormat:@"$%@ == 20", firstRow];

firstRow is the first XLFormRowDescriptor.

This actually creates an NSPredicate that is automatically evaluated whenever values change and sets the visibility appropriately.

Taken from XLForm's example:

For example, you could set the following string to a row (second) to make it disappear when a previous row (first) contains the value "hide".

second.hidden = [NSString stringWithFormat:@"$%d contains[c] 'hide'", [first.value intValue]];

Edit: I changed the predicate to work with NSNumber values.