iOS XLForm速用教程

一 簡述

XLForm 是最靈活且最強(qiáng)大的創(chuàng)建動態(tài)表單的iOS庫。以下是這個庫一個簡單的結(jié)構(gòu)圖:


最主要的是紅色方框的三個類:

XLFormRowDescriptor,XLFormSectionDescriptorXLFormDescriptor。
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];
// 添加一個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對象賦值給父類的form。不然不顯示表單。
運(yùn)行工程,簡單三步一個簡單的表單已然出現(xiàn)。并且XLForm已經(jīng)將你的鍵盤管理的妥妥當(dāng)當(dāng)?shù)?。再也不?dān)心遮擋輸入框了??。

三 自定義Cell

   // 內(nèi)部直接賦值  
NSString * const XLFormRowDescriporTypeFloat = @"XLFormRowDescriporTypeFloat";  
  
@interface MKJFloatTextFieldCell () <UITextFieldDelegate>  
  
@end  
  
@implementation MKJFloatTextFieldCell  
  
// 在主表單中注冊對應(yīng)的cell以及對應(yīng)的ID  
+(void)load  
{  
    [XLFormViewController.cellClassesForRowDescriptorTypes setObject:NSStringFromClass([MKJFloatTextFieldCell class]) forKey:XLFormRowDescriporTypeFloat];  
}  
  
// 這個方法是用來設(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];  
      
}  
// 這個方法是用來進(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)行判斷更新或者移除或者增加
     // 每個cell內(nèi)部的參數(shù)屬性更改了就會調(diào)用這個方法,我們再次更新的話就會調(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:@"這里填寫的就是我們注冊的ID"]){  
//          
//    }  
      
}  
表單填完了,無論自定義還是自帶的,都要搜集填寫的數(shù)據(jù)

1.驗(yàn)證數(shù)據(jù)合法性:[self formValidationErrors];
2.獲取所有數(shù)據(jù):[self formValues];

最后給大家一個cell樣式的總匯

    #import "XLForm.h"  
// JVFloatLabeledTextField 普通的文本輸入框,自帶浮動動畫  
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對應(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";  
// 簡單的右側(cè)描述信息的cell  
NSString *const XLFormRowDescriptorTypeInfo = @"info";  
// 展示segment count計(jì)數(shù)  
NSString *const XLFormRowDescriptorTypeStepCounter = @"stepCounter";  

傳送門 XLForm GitHub

福利

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • HTML表單 在HTML中,表單是 ... 之間元素的集合,它們允許訪問者輸入文本、選擇選項(xiàng)、操作對象等等,然后將...
    蘭山小亭閱讀 3,504評論 2 14
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,177評論 4 61
  • 今天復(fù)習(xí)了元學(xué)習(xí)的第二課,并對知識點(diǎn)進(jìn)行了整理,發(fā)現(xiàn)了好多新的和已經(jīng)忘記了的知識。 先說新手進(jìn)行學(xué)習(xí)的正確步驟: ...
    妄_念閱讀 164評論 0 1
  • 曾經(jīng)喜歡初夏,因?yàn)殛柟夂軠嘏?。如今最愛初夏,因?yàn)槟抢镉心恪? 去年的這個時(shí)候,我應(yīng)該還在一遍一遍數(shù)著那封情書...
    木椋閱讀 1,015評論 0 1
  • 最近被熱播劇《三生三世十里桃花》刷屏了,好不容易等著素素跳了誅仙臺,但是卻開學(xué)了。那么如何有逼格的追劇,就看看《三...
    那個誰513閱讀 465評論 0 0

友情鏈接更多精彩內(nèi)容