因項目需要用到大量的表單提交,最初我是通過自己純代碼敲一個一個控件的敲,隨著表單需求多起來時,代碼量有點大,后來嘗試封裝效果不理想,最終發(fā)現(xiàn)了這個XLForm解決了我心頭之困,在很大程度上提高了表單制作的效率,也十分的靈活。其實網(wǎng)上也有很多關于此框架的文章,但發(fā)現(xiàn)對如何用純代碼自定義cell的表單編寫沒有文章介紹,以下就是我寫的demo中的代碼段的大致使用,有興趣可下載我的demo結合來讀 Demo傳送門
集成框架
如一般第三方框架一樣都需要導入到自己項目中XLForm,但要注意到是在你編寫表單的控制器中需要繼承XLFormViewController這個類
主要代碼說明
<pre>初始化- (instancetype)init
{
self = [super init];
if (self) {
[self initializeForm];
}
return self;
}
要對表單中的行禁止編輯用到的屬性
row.required = YES;
框架自帶的button cell中要想點擊出發(fā)方法用到的屬性
row.action.formSelector = @selector(insertBtnClick);
若要獲取表單中所有cell所填的值用到的方法如下,是以字典的方式保存
NSDictionary *allDict = [self formValues];
</pre>
自定義cell
自定義cell 有兩個很重要的方法需要重寫
<pre>
第一個:
此方法是用來設置屬性的 必須重寫 類似于初始化的屬性不變的屬性進行預先配置,純代碼在cell中添加控件和布局的代碼也是寫在此方法內(nèi)
-(void)configure
{
[super configure];
__weak typeof (self) weakSelf = self;
UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
saveBtn.backgroundColor = [UIColor clearColor];
[saveBtn setBackgroundImage:[UIImage imageNamed:@"btn_green_normal"] forState:UIControlStateNormal];
[saveBtn setTitle:@"保存" forState:UIControlStateNormal];
[weakSelf.contentView addSubview:saveBtn];
[saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(weakSelf.contentView);
make.centerX.equalTo(weakSelf.contentView);
make.width.equalTo(weakSelf.contentView).multipliedBy(0.8);
make.height.equalTo(weakSelf.contentView).multipliedBy(0.8);
}];
[saveBtn addTarget:self action:@selector(saveBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
</pre>
那問題來了,我點擊了保存按鈕,我在控制器中如何拿到保存繼而出發(fā)要執(zhí)行的方法,但這里貌似只能通過通知處理。
<pre>
-(void)saveBtnClick{
[[NSNotificationCenter defaultCenter] postNotificationName:@"updataalterPswNotification" object:self];
}
</pre>
還有就是自定義pickerView選定后如何的數(shù)據(jù)控制器是如何拿到的呢,其實是通過將數(shù)據(jù)存到一個字典中,字典賦值給self.rowDescriptor.value
<pre>
-(void)saveClick{
WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithCompleteBlock:^(NSDate *startDate) {
NSString *date = [startDate stringWithFormat:@"yyyy-MM-dd "];
NSDictionary *dict = [NSDictionary dictionary];
dict = @{
@"data":date,
};
_dataLabel.text = date;
self.rowDescriptor.value = @{@"data":date};
}];
datepicker.datePickerStyle = DateStyleShowYearMonthDay;
datepicker.minLimitDate = [NSDate date:@"1970-1-01 " WithFormat:@"yyyy-MM-dd "];
datepicker.maxLimitDate = [NSDate date:@"2049-12-31 " WithFormat:@"yyyy-MM-dd "];
[datepicker show];
}
</pre>
而在控制器中,用之前說到的方法,就可以很開心的拿到pirkerView這一行的數(shù)據(jù),其它的同理
<pre>
NSDictionary allDict = [self formValues];
</pre>
而點擊了按鈕后如何拿到cell中的值呢
接下來的是另一個重要的方法, 此方法是用來進行更新的,外面給唯一的字段Value設定值就好了 通過self.rowDescriptor.value的值變化來進行更新
<pre>
-(void)update{
[super update];
NSDictionary *value = self.rowDescriptor.value;
NSString *btnTag = [value objectForKey:@"btnTag"];
_btnTag = btnTag;
}
</pre>
此外要想一開始就給自定義cell中一個初始值,也是通過self.rowDescriptor.value進行
首先需要通過字典形式在控制器給需要初始值的cell賦值,如:
<pre>
//生日
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"birthday" rowType:XLFormRowDescriptorTypePickerDataCell title:@"生日:"];
row.disabled = @NO;
row.value = @{@"data":birthday};
[section addFormRow:row];
中的row.value = @{@"data":birthday};這個屬性賦值
/---------------------華麗的分割線------------------------------/
在自定義cell中也通過字典拿
NSDictionary *dict = [NSDictionary dictionary];
dict = @{
@"data":date,
};
_dataLabel.text = date;
self.rowDescriptor.value = @{@"data":date};
</pre>