iOS開(kāi)發(fā)教程之XLForm的基本使用方法
前言
在iOS開(kāi)發(fā)中,開(kāi)發(fā)"表單"界面,字段稍微多一點(diǎn)的一般都用UITableView來(lái)做,而XLForm就是這樣一個(gè)框架,它是創(chuàng)建動(dòng)態(tài)表格視圖最牛逼的iOS庫(kù), 用它實(shí)現(xiàn)表單功能,非常簡(jiǎn)單,省心省力。但是很可惜,搜索了很多文章都只是翻譯官方文檔,很多人在使用該庫(kù)的時(shí)候可能都被官方文檔帶走遠(yuǎn)了,不知道如何具體使用。正好最近也要用到這個(gè)庫(kù),所以寫(xiě)個(gè)入門(mén)使用文章供大家參考。
以下是這個(gè)庫(kù)一個(gè)簡(jiǎn)單的結(jié)構(gòu)圖:

一、 導(dǎo)入項(xiàng)目
使用CocoaPods或者手動(dòng)導(dǎo)入庫(kù)文件,本人選擇直接導(dǎo)入項(xiàng)目源文件的方式。

導(dǎo)入項(xiàng)目
二、改造表單ViewController
讓ViewController繼承自XLFormViewController,并重寫(xiě)下面的兩個(gè)方法
@interface OneViewController : XLFormViewController
@end
@implementation OneViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self){
[self initializeForm];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self){
[self initializeForm];
}
return self;
}
@end
三、構(gòu)造表單
- (void)initializeForm {
// 設(shè)置是否顯示Cell之間分界線
//self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 設(shè)置Section的高度
self.tableView.sectionHeaderHeight = 30;
XLFormDescriptor * form;//form,一個(gè)表單只有一個(gè)
XLFormSectionDescriptor * section;//section,一個(gè)表單可能有多個(gè)
XLFormRowDescriptor * row; //row,每個(gè)section可能有多個(gè)row
// Form
form = [XLFormDescriptor formDescriptor];
// First section
section = [XLFormSectionDescriptor formSection];
section.title = @"用戶";
[form addFormSection:section];
// 普通文本
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"username" rowType:XLFormRowDescriptorTypeText];
// 設(shè)置placeholder
[row.cellConfig setObject:@"用戶名" forKey:@"textField.placeholder"];
// 設(shè)置文本顏色
[row.cellConfig setObject:[UIColor redColor] forKey:@"textField.textColor"];
[section addFormRow:row];
// 密碼
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"password" rowType:XLFormRowDescriptorTypePassword];
// 設(shè)置placeholder的顏色
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"密碼" attributes:
@{NSForegroundColorAttributeName:[UIColor greenColor],
}];
[row.cellConfig setObject:attrString forKey:@"textField.attributedPlaceholder"];
[section addFormRow:row];
// Second Section
section = [XLFormSectionDescriptor formSection];
section.title = @"日期";
[form addFormSection:section];
// 日期選擇器
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"birthday" rowType:XLFormRowDescriptorTypeDate title:@"出生日期"];
row.value = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
[section addFormRow:row];
// Third Section
section = [XLFormSectionDescriptor formSection];
section.title = @"頭像";
[form addFormSection:section];
// 圖片選擇
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userpic" rowType:XLFormRowDescriptorTypeImage];
[section addFormRow:row];
// Fourth Section
section = [XLFormSectionDescriptor formSection];
section.title = @"選擇器";
[form addFormSection:section];
// 選擇器
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sex" rowType:XLFormRowDescriptorTypeSelectorPush];
row.noValueDisplayText = @"暫無(wú)";
row.selectorTitle = @"性別選擇";
row.selectorOptions = @[@"男",@"女",@"其他"];
row.title = @"性別";
[row.cellConfigForSelector setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
[row.cellConfigForSelector setObject:[UIColor greenColor] forKey:@"detailTextLabel.textColor"];
[section addFormRow:row];
// Fifth Section
section = [XLFormSectionDescriptor formSection];
section.title = @"加固";
[form addFormSection:section];
// 開(kāi)關(guān)
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"enforce" rowType:XLFormRowDescriptorTypeBooleanSwitch title:@"加固"];
[section addFormRow:row];
// Sixth Section
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// 按鈕
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"conform" rowType:XLFormRowDescriptorTypeButton];
row.title = @"確定";
[section addFormRow:row];
self.form = form;
}
-(void)didSelectFormRow:(XLFormRowDescriptor *)formRow{
// 判斷是不是點(diǎn)擊了確定按鈕
if([formRow.tag isEqualToString:@"conform"] && formRow.rowType == XLFormRowDescriptorTypeButton){
//獲取表單所有到的值
NSDictionary *values = [self formValues];
NSLog(@"%@", values);
}
[super didSelectFormRow:formRow];
}
//重寫(xiě)改該方法 上面的方法就不會(huì)調(diào)用了
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
// NSLog(@"%s", __func__);
//
//}
@end
四、效果圖

五、總結(jié)
前面兩步是官方文檔中可以找到的,也很簡(jiǎn)單,關(guān)鍵在于initializeForm方法中具體構(gòu)造表單的過(guò)程,這里有必要強(qiáng)調(diào)幾點(diǎn):
1、XLFormViewController實(shí)現(xiàn)了UITableViewDataSource, UITableViewDelegate,并且持有一個(gè)UITableView,這個(gè)從該類的聲明可以看出來(lái),所以UITableView 、UITableViewDataSource, UITableViewDelegate中的方法都可以正常使用。
@interface XLFormViewController : UIViewController<UITableViewDataSource, UITableViewDelegate,XLFormDescriptorDelegate, UITextFieldDelegate, UITextViewDelegate, XLFormViewControllerDelegate>
2、XLForm將表單抽象為Form,Section,Row三個(gè)層次,分別對(duì)應(yīng)三個(gè)類
XLFormDescriptor * form;//form,一個(gè)表單只有一個(gè) XLFormSectionDescriptor * section;//section,一個(gè)表單可能有多個(gè) XLFormRowDescriptor * row; //row,每個(gè)section可能有多個(gè)row
3、每個(gè)表單中的具體信息最后都落腳到XLFormRowDescriptor中,通過(guò)它可以配置不同樣式的表單項(xiàng),通過(guò)構(gòu)造函數(shù)的rowType指定具體的表單類型,該框架提供了非常豐富的rowType,具體可以參考官方文檔說(shuō)明。
4、更細(xì)化配置表單項(xiàng)就需要借助于XLFormRowDescriptor中的屬性進(jìn)行配置,常用的有
@property (nonatomic, readonly, nonnull) NSMutableDictionary * cellConfig; @property (nonatomic, readonly, nonnull) NSMutableDictionary * cellConfigForSelector;
這個(gè)配置的時(shí)候,往往有同學(xué)不知道具體如何才能設(shè)置屬性,比如怎么設(shè)置表單輸入框的placeholder?更進(jìn)一步如何設(shè)置placeholder 的顏色。其實(shí)它用到了KVC,因?yàn)樗鼈儍蓚€(gè)都是UITextField類中的屬性,那么直接進(jìn)入U(xiǎn)ITextField查找,發(fā)現(xiàn)如下信息:
@property(nullable, nonatomic,copy) NSString *placeholder; @property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0);
那么設(shè)置起來(lái)就是
[row.cellConfig setObject:@"用戶名" forKey:@"textField.placeholder"]; [row.cellConfig setObject:attrString forKey:@"textField.attributedPlaceholder"];
注意這里的key的寫(xiě)法,就是KVC的寫(xiě)法。其他的屬性依此類推。
5、如何獲取設(shè)置好的表單的值?其實(shí)非常簡(jiǎn)單,該框架提供一個(gè)方法formValues,它的返回類型是一個(gè)NSDictionary,其中key就是XLFormRowDescriptor設(shè)置時(shí)的Tag??梢灾苯釉诳刂破髦姓{(diào)用該方法獲取表單值,上面的效果圖設(shè)置后的表單信息如下:

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
IOS實(shí)現(xiàn)展開(kāi)二級(jí)列表效果
本文通過(guò)實(shí)例代碼向大家演示在IOS中如何實(shí)現(xiàn)展開(kāi)二級(jí)列表的效果,這個(gè)功能效果很好,對(duì)于日常開(kāi)發(fā)APP中很有幫助,下面一起來(lái)看看如何實(shí)現(xiàn)吧。2016-08-08
iOS通過(guò)block在兩個(gè)頁(yè)面間傳值的方法
不知道大家有沒(méi)有發(fā)現(xiàn),在實(shí)際開(kāi)發(fā)中使用block的地方特別多,block比delegate和notification有著更簡(jiǎn)潔的優(yōu)勢(shì),下面這篇文章我們來(lái)簡(jiǎn)單了解一下block在兩個(gè)頁(yè)面之間的傳值。有需要的朋友們可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
iOS開(kāi)發(fā)之路--仿網(wǎng)易抽屜效果
本文是IOS開(kāi)發(fā)之路系列的第一篇,主要講訴了如何仿網(wǎng)易新聞客戶端實(shí)現(xiàn)抽屜效果,全部源代碼都分享給大家,希望對(duì)大家有所幫助2014-08-08
IOS Cache設(shè)計(jì)詳細(xì)介紹及簡(jiǎn)單示例
這篇文章主要介紹了IOS Cache設(shè)計(jì)詳細(xì)介紹及簡(jiǎn)單示例的相關(guān)資料,Cache的目的是為了追求更高的速度體驗(yàn),Cache的源頭是兩種數(shù)據(jù)讀取方式在成本和性能上的差異,需要的朋友可以參考下2017-01-01
iPhone/iPad開(kāi)發(fā)通過(guò)LocalNotification實(shí)現(xiàn)iOS定時(shí)本地推送功能
這篇文章主要介紹了iPhone/iPad開(kāi)發(fā)之通過(guò)LocalNotification實(shí)現(xiàn)iOS定時(shí)本地推送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
iOS App開(kāi)發(fā)中UISearchBar搜索欄組件的基本用法整理
iOS開(kāi)發(fā)組件中自帶的UISearchBar提供了很多基礎(chǔ)和好用的搜索欄UI功能,下面就來(lái)總結(jié)一下iOS App開(kāi)發(fā)中UISearchBar搜索欄組件的基本用法整理,需要的朋友可以參考下2016-05-05

