Search code examples
objective-cxlform

disabling rows on condition on xlform


I have a section set up using xlform where there is a bool field and then a text field under it. I want to disable the text field if the bool is chosen. I tried with the following, but it did not work

if ([[self.form valueForKey:@"pay"] isEqualToValue:@(1)]){
    row.disabled = YES;
} else {
    row.required = YES;
}
[section addFormRow:row];

Any suggestions? Here is the documentation, spent a bunch of time search without being able to find the answer.

Edit: I'm starting to think that the values of the dictionary don't get updated dynamically. Which is odd because the dictionary can be accessed in other parts of the view controller at any time.

Edit

- (void)formRowDescriptorValueHasChangedTwo:(XLFormRowDescriptor *)formRow value:(id)value
{
[value isEqual:[self.formValues valueForKey:@"pay"]];

if([formRow.tag isEqualToString:@"pay"] && [value isEqual:[NSNumber numberWithBool:YES]])
{
    self.price.disabled = NO;
    self.price.required = YES;
    [self.tableView reloadRowsAtIndexPaths:@[[self.form indexPathOfFormRow:self.price]]
                          withRowAnimation:UITableViewRowAnimationNone];
}
}

I got it working, but I have a new problem.

Here is the code - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)formRow oldValue:(id)oldValue newValue:(id)newValue {

if([formRow.tag isEqualToString:@"now"] && [newValue isEqual:[NSNumber numberWithBool:YES]])
{
    self.time.disabled = YES;
    [self.tableView reloadRowsAtIndexPaths:@[[self.form indexPathOfFormRow:self.time]]
                          withRowAnimation:UITableViewRowAnimationAutomatic];
}



    if([formRow.tag isEqualToString:@"pay"] && [newValue isEqual:[NSNumber numberWithBool:YES]])
{
    self.price.disabled = NO;
    self.price.required = YES;
    [self.tableView reloadRowsAtIndexPaths:@[[self.form indexPathOfFormRow:self.price]]
                          withRowAnimation:UITableViewRowAnimationAutomatic];
}




}

When I click on the first row (now), it disables the corresponding row fine, but when I click on the second row (pay), it makes the row that now disables reappear AND it makes the row that I want to appear under pay to disappear.

Edit Got it to work by changing the animation to UITableViewRowAnimationNone


Solution

  • Here's how you can do it using XLFormDescriptorDelegate. This will change the state as the field is toggled on the form.

    @interface SomeClass() <XLFormDescriptorDelegate>
    @end
    
    @implementation SomeClass
    
    - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)formRow oldValue:(id)oldValue newValue:(id)newValue
    {
    
        if([formRow.tag isEqualToString:@"boolFieldTag"] && [newValue boolValue])
        {
            self.otherRow.disabled = YES;
            [self.tableView reloadRowsAtIndexPaths:@[[self.form indexPathOfFormRow:self.otherRow]]
                                  withRowAnimation:UITableViewRowAnimationNone];
        }
    }
    
    @end
    

    In this example I set the row as a property when I originally added it to the form. Alternatively you can just use: [self.form formRowWithTag:@"someTag"]