diff --git a/Examples/Objective-C/Examples/MultiValuedSections/MultivaluedFormViewController.m b/Examples/Objective-C/Examples/MultiValuedSections/MultivaluedFormViewController.m index 05591e03..30519620 100644 --- a/Examples/Objective-C/Examples/MultiValuedSections/MultivaluedFormViewController.m +++ b/Examples/Objective-C/Examples/MultiValuedSections/MultivaluedFormViewController.m @@ -71,6 +71,18 @@ - (id)init row.selectorOptions = @[@"Option 1", @"Option 2", @"Option 3"]; section.multivaluedRowTemplate = [row copy]; [section addFormRow:row]; + + + // Add Section Button + section = [XLFormSectionDescriptor formSection]; + [form addFormSection:section]; + row = [XLFormRowDescriptor formRowDescriptorWithTag:@"AddSectionButton" rowType:XLFormRowDescriptorTypeButton title:@"Add Section"]; + row.action.formSelector = @selector(didTouchAddSectionButton:); + [row.cellConfigAtConfigure setObject:[UIColor clearColor] forKey:@"backgroundColor"]; + [row.cellConfig setObject:[UIColor grayColor] forKey:@"textLabel.color"]; + [row.cellConfig setObject:[UIFont fontWithName:@"Helvetica" size:17] forKey:@"textLabel.font"]; + [section addFormRow:row]; + return [super initWithForm:form]; } @@ -90,6 +102,25 @@ -(void)addDidTouch:(UIBarButtonItem * __unused)sender } +-(void)didTouchAddSectionButton:(XLFormRowDescriptor *)sender +{ + // Create a new section + XLFormSectionDescriptor * newSection = [XLFormSectionDescriptor formSectionWithTitle:[NSString stringWithFormat:@"Section created at %@", [NSDateFormatter localizedStringFromDate:[NSDate new] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]]]; + newSection.multivaluedTag = [NSString stringWithFormat:@"multivaluedPushSelector_%@", @(self.form.formSections.count-1)]; + + // Add rows to the new section + XLFormRowDescriptor * newRow = [XLFormRowDescriptor formRowDescriptorWithTag:nil rowType:XLFormRowDescriptorTypeSelectorPush title:@"Language"]; + newRow.selectorOptions = @[@"Option 1", @"Option 2", @"Option 3"]; + [newSection addFormRow:newRow]; + newRow = [XLFormRowDescriptor formRowDescriptorWithTag:nil rowType:XLFormRowDescriptorTypeSelectorPush title:@"Level"]; + newRow.selectorOptions = @[@"Option A", @"Option B", @"Option C"]; + [newSection addFormRow:newRow]; + + // Add new section + [self.form addFormSection:newSection beforeSection:sender.sectionDescriptor]; + [self deselectFormRow:sender]; +} + #pragma mark - UIActionSheetDelegate - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex @@ -116,6 +147,7 @@ - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger } } + @end diff --git a/README.md b/README.md index 63f62855..1398fed9 100755 --- a/README.md +++ b/README.md @@ -900,9 +900,9 @@ Version 3.0.0 (master) * `hidden` property added to `XLFormSectionDescriptor`. `@YES` `@NO` or a `NSPredicate` can be used to hide the section. * Added `XLFormRowDescriptorTypeCountDownTimerInline` and `XLFormRowDescriptorTypeCountDownTimer` row type with an example. * Deleted `dateFormatter` property and added support to use the `NSValueTransformer` to convert the selected object to a NSString in the XLFormDateCell class. - * Added `XLFormRowDescriptorTypeCountDownTimerInline` and `XLFormRowDescriptorTypeCountDownTimer` row type with an example. * Deleted `dateFormatter` property and added support to use the `NSValueTransformer` to convert the selected object to a NSString in the XLFormDateCell class. +* Added example in the ```Multivalued Sections examples``` of how to add dynamic sections with multiple rows. Version 2.2.0 diff --git a/XLForm/XL/Descriptors/XLFormDescriptor.h b/XLForm/XL/Descriptors/XLFormDescriptor.h index 4038f7ec..5a692939 100644 --- a/XLForm/XL/Descriptors/XLFormDescriptor.h +++ b/XLForm/XL/Descriptors/XLFormDescriptor.h @@ -64,6 +64,7 @@ typedef NS_OPTIONS(NSUInteger, XLFormRowNavigationOptions) { -(void)addFormSection:(XLFormSectionDescriptor *)formSection; -(void)addFormSection:(XLFormSectionDescriptor *)formSection atIndex:(NSUInteger)index; +-(void)addFormSection:(XLFormSectionDescriptor *)formSection beforeSection:(XLFormSectionDescriptor *)beforeSection; -(void)addFormSection:(XLFormSectionDescriptor *)formSection afterSection:(XLFormSectionDescriptor *)afterSection; -(void)addFormRow:(XLFormRowDescriptor *)formRow beforeRow:(XLFormRowDescriptor *)afterRow; -(void)addFormRow:(XLFormRowDescriptor *)formRow beforeRowTag:(NSString *)afterRowTag; diff --git a/XLForm/XL/Descriptors/XLFormDescriptor.m b/XLForm/XL/Descriptors/XLFormDescriptor.m index 409f2e3d..01ef2490 100644 --- a/XLForm/XL/Descriptors/XLFormDescriptor.m +++ b/XLForm/XL/Descriptors/XLFormDescriptor.m @@ -126,6 +126,23 @@ -(void)addFormSection:(XLFormSectionDescriptor *)formSection afterSection:(XLFor formSection.hidden = formSection.hidden; } +-(void)addFormSection:(XLFormSectionDescriptor *)formSection beforeSection:(XLFormSectionDescriptor *)afterSection +{ + NSUInteger sectionIndex; + NSUInteger allSectionIndex; + if ((sectionIndex = [self.allSections indexOfObject:formSection]) == NSNotFound){ + allSectionIndex = [self.allSections indexOfObject:afterSection]; + if (allSectionIndex != NSNotFound) { + [self insertObject:formSection inAllSectionsAtIndex:allSectionIndex]; + } + else { //case when afterSection does not exist. Just insert at the end. + [self addFormSection:formSection]; + return; + } + } + formSection.hidden = formSection.hidden; +} + -(void)addFormRow:(XLFormRowDescriptor *)formRow beforeRow:(XLFormRowDescriptor *)beforeRow {