Skip to content

Commit

Permalink
Added custom inline row example in Objective C and Swift.
Browse files Browse the repository at this point in the history
  • Loading branch information
mats-claassen committed Dec 17, 2015
1 parent 9de9b69 commit 7adfdb3
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
#import "XLFormRatingCell.h"
#import "FloatLabeledTextFieldCell.h"
#import "XLFormCustomCell.h"
#import "XLFormInlineSegmentedCell.h"

static NSString * const kCustomRowFirstRatingTag = @"CustomRowFirstRatingTag";
static NSString * const kCustomRowSecondRatingTag = @"CustomRowSecondRatingTag";
static NSString * const kCustomRowFloatLabeledTextFieldTag = @"CustomRowFloatLabeledTextFieldTag";
static NSString * const kCustomRowWeekdays = @"CustomRowWeekdays";
static NSString * const kCustomInline = @"kCustomInline";
static NSString * const kCustomRowText = @"kCustomText";

@implementation CustomRowsViewController
Expand Down Expand Up @@ -94,6 +96,16 @@ -(void)initializeForm
};
[section addFormRow:row];

// Custom Inline Segmented row
section = [XLFormSectionDescriptor formSectionWithTitle:@"Custom Inline"];
[form addFormSection:section];

// Inline segmented
row = [XLFormRowDescriptor formRowDescriptorWithTag:kCustomInline rowType:XLFormRowDescriptorTypeSegmentedInline];
row.title = @"You support...";
row.selectorOptions = @[@"Uruguay", @"Brazil", @"Argentina", @"Chile"];
row.value = @"Uruguay";
[section addFormRow:row];

section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
Expand Down
22 changes: 22 additions & 0 deletions Examples/Objective-C/Inline Segmented/XLFormInlineSegmentedCell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// XLFormInlineSegmentedCell.h
// XLForm
//
// Created by mathias Claassen on 16/12/15.
// Copyright © 2015 Xmartlabs. All rights reserved.
//

#import <XLForm/XLForm.h>

extern NSString * const XLFormRowDescriptorTypeSegmentedInline;
extern NSString * const XLFormRowDescriptorTypeSegmentedControl;

@interface XLFormInlineSegmentedCell : XLFormBaseCell

@end


@interface XLFormInlineSegmentedControl : XLFormBaseCell<XLFormInlineRowDescriptorCell>

@property (strong, nonatomic) UISegmentedControl* segmentedControl;
@end
194 changes: 194 additions & 0 deletions Examples/Objective-C/Inline Segmented/XLFormInlineSegmentedCell.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
//
// XLFormInlineSegmentedCell.m
// XLForm
//
// Created by mathias Claassen on 16/12/15.
// Copyright © 2015 Xmartlabs. All rights reserved.
//

#import "XLFormInlineSegmentedCell.h"

NSString * const XLFormRowDescriptorTypeSegmentedInline = @"XLFormRowDescriptorTypeSegmentedInline";
NSString * const XLFormRowDescriptorTypeSegmentedControl = @"XLFormRowDescriptorTypeSegmentedControl";

@implementation XLFormInlineSegmentedCell

+(void)load
{
[XLFormViewController.cellClassesForRowDescriptorTypes setObject:[XLFormInlineSegmentedCell class] forKey:XLFormRowDescriptorTypeSegmentedInline];
[XLFormViewController.inlineRowDescriptorTypesForRowDescriptorTypes setObject:XLFormRowDescriptorTypeSegmentedControl forKey:XLFormRowDescriptorTypeSegmentedInline];
}

- (BOOL)canBecomeFirstResponder
{
return YES;
}

-(BOOL)becomeFirstResponder
{
if (self.isFirstResponder){
return [super becomeFirstResponder];
}
BOOL result = [super becomeFirstResponder];
if (result){
XLFormRowDescriptor * inlineRowDescriptor = [XLFormRowDescriptor formRowDescriptorWithTag:nil rowType:[XLFormViewController inlineRowDescriptorTypesForRowDescriptorTypes][self.rowDescriptor.rowType]];
UITableViewCell<XLFormDescriptorCell> * cell = [inlineRowDescriptor cellForFormController:self.formViewController];
NSAssert([cell conformsToProtocol:@protocol(XLFormInlineRowDescriptorCell)], @"inline cell must conform to XLFormInlineRowDescriptorCell");
UITableViewCell<XLFormInlineRowDescriptorCell> * inlineCell = (UITableViewCell<XLFormInlineRowDescriptorCell> *)cell;
inlineCell.inlineRowDescriptor = self.rowDescriptor;
[self.rowDescriptor.sectionDescriptor addFormRow:inlineRowDescriptor afterRow:self.rowDescriptor];
[self.formViewController ensureRowIsVisible:inlineRowDescriptor];
}
return result;
}

-(BOOL)resignFirstResponder
{
if (![self isFirstResponder]) {
return [super resignFirstResponder];
}
NSIndexPath * selectedRowPath = [self.formViewController.form indexPathOfFormRow:self.rowDescriptor];
NSIndexPath * nextRowPath = [NSIndexPath indexPathForRow:selectedRowPath.row + 1 inSection:selectedRowPath.section];
XLFormRowDescriptor * nextFormRow = [self.formViewController.form formRowAtIndex:nextRowPath];
XLFormSectionDescriptor * formSection = [self.formViewController.form.formSections objectAtIndex:nextRowPath.section];
BOOL result = [super resignFirstResponder];
if (result) {
[formSection removeFormRow:nextFormRow];
}
return result;
}


#pragma mark - XLFormDescriptorCell

-(void)configure
{
[super configure];
}

-(void)update
{
[super update];
self.accessoryType = UITableViewCellAccessoryNone;
self.editingAccessoryType = UITableViewCellAccessoryNone;
[self.textLabel setText:self.rowDescriptor.title];
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.detailTextLabel.text = [self valueDisplayText];
}

-(BOOL)formDescriptorCellCanBecomeFirstResponder
{
return !(self.rowDescriptor.isDisabled);
}

-(BOOL)formDescriptorCellBecomeFirstResponder
{

if ([self isFirstResponder]){
[self resignFirstResponder];
return NO;
}
return [self becomeFirstResponder];
}

-(void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
{
[controller.tableView deselectRowAtIndexPath:[controller.form indexPathOfFormRow:self.rowDescriptor] animated:YES];
}

#pragma mark - Helpers

-(NSString *)valueDisplayText
{
return (self.rowDescriptor.value ? [self.rowDescriptor.value displayText] : self.rowDescriptor.noValueDisplayText);
}



@end




@implementation XLFormInlineSegmentedControl

@synthesize segmentedControl = _segmentedControl;
@synthesize inlineRowDescriptor = _inlineRowDescriptor;


+(void)load
{
[XLFormViewController.cellClassesForRowDescriptorTypes setObject:[XLFormInlineSegmentedControl class] forKey:XLFormRowDescriptorTypeSegmentedControl];
}

-(void)configure
{
[super configure];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self.contentView addSubview:self.segmentedControl];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[segmentedControl]-|"
options:NSLayoutFormatAlignAllCenterY
metrics:0
views:@{@"segmentedControl": self.segmentedControl}]];
[self.segmentedControl addTarget:self action:@selector(valueChanged) forControlEvents:UIControlEventValueChanged];
}

-(void)update
{
[super update];
[self updateSegmentedControl];
self.segmentedControl.selectedSegmentIndex = [self selectedIndex];
self.segmentedControl.enabled = !self.rowDescriptor.isDisabled;
}

-(UISegmentedControl *)segmentedControl
{
if (_segmentedControl) return _segmentedControl;

_segmentedControl = [UISegmentedControl autolayoutView];
[_segmentedControl setContentHuggingPriority:500 forAxis:UILayoutConstraintAxisHorizontal];
return _segmentedControl;
}


#pragma mark - Action

-(void)valueChanged
{
self.inlineRowDescriptor.value = [self.inlineRowDescriptor.selectorOptions objectAtIndex:self.segmentedControl.selectedSegmentIndex];
[self.formViewController updateFormRow:self.inlineRowDescriptor];
}

#pragma mark - Helper

-(NSArray *)getItems
{
NSMutableArray * result = [[NSMutableArray alloc] init];
for (id option in self.inlineRowDescriptor.selectorOptions)
[result addObject:[option displayText]];
return result;
}

-(void)updateSegmentedControl
{
[self.segmentedControl removeAllSegments];

[[self getItems] enumerateObjectsUsingBlock:^(id object, NSUInteger idex, __unused BOOL *stop){
[self.segmentedControl insertSegmentWithTitle:[object displayText] atIndex:idex animated:NO];
}];
}

-(NSInteger)selectedIndex
{
XLFormRowDescriptor * formRow = self.inlineRowDescriptor ?: self.rowDescriptor;
if (formRow.value){
for (id option in formRow.selectorOptions){
if ([[option valueData] isEqual:[formRow.value valueData]]){
return [formRow.selectorOptions indexOfObject:option];
}
}
}
return -1;
}

@end
14 changes: 14 additions & 0 deletions Examples/Objective-C/XLForm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
3C3B01F01AB74BDC0027CD45 /* FloatLabeledTextFieldCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C3B01EF1AB74BDC0027CD45 /* FloatLabeledTextFieldCell.m */; };
3CDAFC7A1AB0AFA4000F75B6 /* CustomRowsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3CDAFC791AB0AFA4000F75B6 /* CustomRowsViewController.m */; };
66B6266E1AE0055100007886 /* DateAndTimeValueTrasformer.m in Sources */ = {isa = PBXBuildFile; fileRef = 66B6266D1AE0055100007886 /* DateAndTimeValueTrasformer.m */; };
BF01E9281C21F0BB00BDE045 /* XLFormInlineSegmentedCell.m in Sources */ = {isa = PBXBuildFile; fileRef = BF01E9271C21F0BB00BDE045 /* XLFormInlineSegmentedCell.m */; };
BF9DB1D51AE0436600B985E7 /* BlogExampleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = BF9DB1D21AE0436600B985E7 /* BlogExampleViewController.m */; };
BF9DB1D61AE0436600B985E7 /* PredicateFormViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = BF9DB1D41AE0436600B985E7 /* PredicateFormViewController.m */; };
BFA5F4B11BB48F8E00D045BD /* XLFormRatingCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3C3B01D41AB741EF0027CD45 /* XLFormRatingCell.xib */; };
Expand Down Expand Up @@ -114,6 +115,8 @@
66B6266D1AE0055100007886 /* DateAndTimeValueTrasformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = DateAndTimeValueTrasformer.m; path = Examples/Dates/DateAndTimeValueTrasformer.m; sourceTree = "<group>"; };
7B0D2D6A86E2A41ED22E8A35 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
976A33EE62A018A7257B4878 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
BF01E9261C21F0BB00BDE045 /* XLFormInlineSegmentedCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = XLFormInlineSegmentedCell.h; path = "Inline Segmented/XLFormInlineSegmentedCell.h"; sourceTree = "<group>"; };
BF01E9271C21F0BB00BDE045 /* XLFormInlineSegmentedCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = XLFormInlineSegmentedCell.m; path = "Inline Segmented/XLFormInlineSegmentedCell.m"; sourceTree = "<group>"; };
BF9DB1D11AE0436600B985E7 /* BlogExampleViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = BlogExampleViewController.h; path = Examples/PredicateExamples/BlogExampleViewController.h; sourceTree = SOURCE_ROOT; };
BF9DB1D21AE0436600B985E7 /* BlogExampleViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = BlogExampleViewController.m; path = Examples/PredicateExamples/BlogExampleViewController.m; sourceTree = SOURCE_ROOT; };
BF9DB1D31AE0436600B985E7 /* PredicateFormViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PredicateFormViewController.h; path = Examples/PredicateExamples/PredicateFormViewController.h; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -416,6 +419,7 @@
3CDAFC741AB0AEE5000F75B6 /* CustomRows */ = {
isa = PBXGroup;
children = (
BF01E9251C21F02D00BDE045 /* Inline Segmented */,
3C3B01D01AB741C40027CD45 /* Rating */,
3C3B01E31AB74AC10027CD45 /* FloatLabeledTextField */,
3C3B01D11AB741CC0027CD45 /* Weekdays */,
Expand All @@ -425,6 +429,15 @@
name = CustomRows;
sourceTree = "<group>";
};
BF01E9251C21F02D00BDE045 /* Inline Segmented */ = {
isa = PBXGroup;
children = (
BF01E9261C21F0BB00BDE045 /* XLFormInlineSegmentedCell.h */,
BF01E9271C21F0BB00BDE045 /* XLFormInlineSegmentedCell.m */,
);
name = "Inline Segmented";
sourceTree = "<group>";
};
BFD5D6F41AD2FFC1006F04FA /* PredicateExamples */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -590,6 +603,7 @@
3C3B01E21AB7499A0027CD45 /* XLRatingView.m in Sources */,
283B59B219532415000828CD /* MapViewController.m in Sources */,
2843EB4718D4915800F13E2B /* ExamplesFormViewController.m in Sources */,
BF01E9281C21F0BB00BDE045 /* XLFormInlineSegmentedCell.m in Sources */,
BF9DB1D61AE0436600B985E7 /* PredicateFormViewController.m in Sources */,
BFE91AFB1AE159B200DE5231 /* BasicPredicateViewController.m in Sources */,
2843EB5218D4CFC700F13E2B /* OthersFormViewController.m in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions Examples/Swift/Examples/CustomRows/CustomRowsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CustomRowsViewController : XLFormViewController {
static let CustomRowFloatLabeledTextFieldTag = "CustomRowFloatLabeledTextFieldTag"
static let CustomRowWeekdays = "CustomRowWeekdays"
static let CustomRowText = "CustomText"
static let CustomRowInline = "CustomRowInline"
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
Expand Down Expand Up @@ -92,6 +93,15 @@ class CustomRowsViewController : XLFormViewController {
]
section.addFormRow(row)

section = XLFormSectionDescriptor.formSectionWithTitle("Custom inline row")
form.addFormSection(section)

// Inline
row = XLFormRowDescriptor(tag: Tags.CustomRowInline, rowType: XLFormRowDescriptorTypeSegmentedInline)
row.title = "You support..."
row.selectorOptions = ["Uruguay", "Brazil", "Argentina", "Chile"]
row.value = "Uruguay"
section.addFormRow(row)

section = XLFormSectionDescriptor()
form.addFormSection(section)
Expand Down
Loading

0 comments on commit 7adfdb3

Please sign in to comment.