一 簡(jiǎn)述
XLForm 是最靈活且最強(qiáng)大的創(chuàng)建動(dòng)態(tài)表單的iOS庫撤缴。以下是這個(gè)庫一個(gè)簡(jiǎn)單的結(jié)構(gòu)圖:
最主要的是紅色方框的三個(gè)類:
XLFormRowDescriptor
娃循,XLFormSectionDescriptor
方援,XLFormDescriptor
庄撮。
1.XLFormDescriptor
結(jié)構(gòu)和UITablView一樣,有Section师倔,有Row悔叽,它就是為成為UITableView的數(shù)據(jù)源而設(shè)計(jì)的莱衩。
2.XLFormRowDescriptor
定義了每行表單的數(shù)據(jù)內(nèi)容,包括行樣式娇澎,標(biāo)題笨蚁,行類型,選擇項(xiàng)內(nèi)容趟庄,標(biāo)簽括细,合法性驗(yàn)證等。
3.XLFormSectionDescriptor
是由XLFormRowDescriptor
組合而成的戚啥,而XLFormSectionDescriptor
最終又組成了XLFormDescriptor
奋单。
二 用法
1.創(chuàng)建繼承于XLFormViewController
的ViewController。
2.創(chuàng)建表格:
XLFormDescriptor * form;//建立表單猫十,等同于創(chuàng)建uitableview
XLFormSectionDescriptor * section;//建立組 section
XLFormRowDescriptor * row;//建立行相當(dāng)于cell
//先將組添加到表單
//設(shè)置標(biāo)題
form = [XLFormDescriptor formDescriptorWithTitle:@"Add Event"];
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// 添加一個(gè)cell
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"Title" rowType:XLFormRowDescriptorTypeText];
//設(shè)置placeholder
[row.cellConfigAtConfigure setObject:@"Title" forKey:@"textField.placeholder"];
row.required = YES;
[section addFormRow:row];
3.最后一步:
self.form = form;
// 把form對(duì)象賦值給父類的form览濒。不然不顯示表單呆盖。
運(yùn)行工程,簡(jiǎn)單三步一個(gè)簡(jiǎn)單的表單已然出現(xiàn)贷笛。并且XLForm已經(jīng)將你的鍵盤管理的妥妥當(dāng)當(dāng)?shù)挠τ帧T僖膊粨?dān)心遮擋輸入框了??。
三 自定義Cell
// 內(nèi)部直接賦值
NSString * const XLFormRowDescriporTypeFloat = @"XLFormRowDescriporTypeFloat";
@interface MKJFloatTextFieldCell () <UITextFieldDelegate>
@end
@implementation MKJFloatTextFieldCell
// 在主表單中注冊(cè)對(duì)應(yīng)的cell以及對(duì)應(yīng)的ID
+(void)load
{
[XLFormViewController.cellClassesForRowDescriptorTypes setObject:NSStringFromClass([MKJFloatTextFieldCell class]) forKey:XLFormRowDescriporTypeFloat];
}
// 這個(gè)方法是用來設(shè)置屬性的 必須重寫 類似于初始化的屬性不變的屬性進(jìn)行預(yù)先配置
- (void)configure
{
[super configure];
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.leftLabel.layer.borderColor = [UIColor yellowColor].CGColor;
self.leftLabel.layer.borderWidth = 1.0f;
self.textField.delegate = self;
self.textField.font = [UIFont boldSystemFontOfSize:16];
self.textField.floatingLabel.font = [UIFont boldSystemFontOfSize:11];
self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
self.textField.floatingLabelTextColor = [UIColor lightGrayColor];
self.textField.floatingLabelActiveTextColor = [UIColor redColor];
}
// 這個(gè)方法是用來進(jìn)行更新的乏苦,外面給唯一的字段Value設(shè)定值就好了 通過self.rowDescriptor.value的值變化來進(jìn)行更新
- (void)update
{
[super update];
NSDictionary *value = self.rowDescriptor.value;
self.leftLabel.text = [value objectForKey:@"left"];
self.textField.text = [value objectForKey:@"right"];
self.textField.attributedPlaceholder =
[[NSAttributedString alloc] initWithString:self.rowDescriptor.title
attributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]}];
self.textField.floatingLabel.text = @"快點(diǎn)輸入面積大小";
}
有些特定事件丁频,需要在VC里面進(jìn)行判斷更新或者移除或者增加
// 每個(gè)cell內(nèi)部的參數(shù)屬性更改了就會(huì)調(diào)用這個(gè)方法,我們?cè)俅胃碌脑捑蜁?huì)調(diào)用cell里面update的方法進(jìn)行重繪
- (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)formRow oldValue:(id)oldValue newValue:(id)newValue
{
// 咱們這里統(tǒng)一調(diào)用更新
[super formRowDescriptorValueHasChanged:formRow oldValue:oldValue newValue:newValue];
[self updateFormRow:formRow];
// 以下就是一些典型的tag判斷邑贴,根據(jù)不同的cell,remove 或 update進(jìn)行更改
// if ([rowDescriptor.tag isEqualToString:@"first"]){
//
// }
// else if ([rowDescriptor.tag isEqualToString:@"second"]){
//
// [self updateFormRow:startDateDescriptor];
// [self updateFormRow:endDateDescriptor];
// }
// else if ([rowDescriptor.tag isEqualToString:@"third"]){
//
// [self updateFormRow:endDateDescriptor];
//
// }
// else if ([rowDescriptor.tag isEqualToString:@"這里填寫的就是我們注冊(cè)的ID"]){
//
// }
}
表單填完了叔磷,無論自定義還是自帶的拢驾,都要搜集填寫的數(shù)據(jù)
1.驗(yàn)證數(shù)據(jù)合法性:[self formValidationErrors];
2.獲取所有數(shù)據(jù):[self formValues];
最后給大家一個(gè)cell樣式的總匯
#import "XLForm.h"
// JVFloatLabeledTextField 普通的文本輸入框,自帶浮動(dòng)動(dòng)畫
NSString *const XLFormRowDescriptorTypeText = @"text";
// add的時(shí)候展示名字的 JVFloatLabeledTextField
NSString *const XLFormRowDescriptorTypeName = @"name";
// 填寫URL的cell
NSString *const XLFormRowDescriptorTypeURL = @"url";
NSString *const XLFormRowDescriptorTypeEmail = @"email";
NSString *const XLFormRowDescriptorTypePassword = @"password";
NSString *const XLFormRowDescriptorTypeNumber = @"number";
NSString *const XLFormRowDescriptorTypePhone = @"phone";
NSString *const XLFormRowDescriptorTypeTwitter = @"twitter";
NSString *const XLFormRowDescriptorTypeAccount = @"account";
NSString *const XLFormRowDescriptorTypeInteger = @"integer";
// 選擇更換頭像圖片的cell
NSString *const XLFormRowDescriptorTypeImage = @"image";
NSString *const XLFormRowDescriptorTypeDecimal = @"decimal";
// JVFloat對(duì)應(yīng)的textView的cell
NSString *const XLFormRowDescriptorTypeTextView = @"textView";
NSString *const XLFormRowDescriptorTypeZipCode = @"zipCode";
// 非常普通的點(diǎn)擊push選擇
NSString *const XLFormRowDescriptorTypeSelectorPush = @"selectorPush";
NSString *const XLFormRowDescriptorTypeSelectorPopover = @"selectorPopover";
NSString *const XLFormRowDescriptorTypeSelectorActionSheet = @"selectorActionSheet";
NSString *const XLFormRowDescriptorTypeSelectorAlertView = @"selectorAlertView";
NSString *const XLFormRowDescriptorTypeSelectorPickerView = @"selectorPickerView";
NSString *const XLFormRowDescriptorTypeSelectorPickerViewInline = @"selectorPickerViewInline";
NSString *const XLFormRowDescriptorTypeMultipleSelector = @"multipleSelector";
NSString *const XLFormRowDescriptorTypeMultipleSelectorPopover = @"multipleSelectorPopover";
NSString *const XLFormRowDescriptorTypeSelectorLeftRight = @"selectorLeftRight";
// 三段選擇
NSString *const XLFormRowDescriptorTypeSelectorSegmentedControl = @"selectorSegmentedControl";
// date 月 日 年 (內(nèi)嵌)
NSString *const XLFormRowDescriptorTypeDateInline = @"dateInline";
// 日期picker選擇器內(nèi)嵌 dateTime更詳細(xì) 星期 月 日 小時(shí) 分(內(nèi)嵌)
NSString *const XLFormRowDescriptorTypeDateTimeInline = @"datetimeInline";
// date 小時(shí) 分 AM/PM(內(nèi)嵌)
NSString *const XLFormRowDescriptorTypeTimeInline = @"timeInline";
// 計(jì)時(shí)器改基,選擇hh mm(內(nèi)嵌)
NSString *const XLFormRowDescriptorTypeCountDownTimerInline = @"countDownTimerInline";
// 月 日 年 sheet
NSString *const XLFormRowDescriptorTypeDate = @"date";
// 最詳細(xì)的dateTime sheet
NSString *const XLFormRowDescriptorTypeDateTime = @"datetime";
// 小時(shí) 分 AM/PM sheet
NSString *const XLFormRowDescriptorTypeTime = @"time";
// 計(jì)時(shí)器 底部彈出來的
NSString *const XLFormRowDescriptorTypeCountDownTimer = @"countDownTimer";
// 直接展示一大坨datePicker
NSString *const XLFormRowDescriptorTypeDatePicker = @"datePicker";
NSString *const XLFormRowDescriptorTypePicker = @"picker";
// slider
NSString *const XLFormRowDescriptorTypeSlider = @"slider";
// 展示選中打鉤的cell
NSString *const XLFormRowDescriptorTypeBooleanCheck = @"booleanCheck";
// 自帶右邊switch開關(guān)
NSString *const XLFormRowDescriptorTypeBooleanSwitch = @"booleanSwitch";
// button的cell 各種button位置需求
NSString *const XLFormRowDescriptorTypeButton = @"button";
// 簡(jiǎn)單的右側(cè)描述信息的cell
NSString *const XLFormRowDescriptorTypeInfo = @"info";
// 展示segment count計(jì)數(shù)
NSString *const XLFormRowDescriptorTypeStepCounter = @"stepCounter";
傳送門 XLForm GitHub