最近公司要做新項目峻凫,趁著現在不忙趕緊將一些知識點提前儲備下蒋搜,由于產品是一款安全產品躬充,需要輸入好多信息而且產品的需求又非常細,所以想講頁面做成那種可以配置的脾歧,期初簡直是讓我蛋都碎了甲捏,不過還好有強大的github,找到了這個XLForm鞭执,它可以方便的進行配置界面省時省力司顿,所以將了解到的東西趕緊記下來,以后好方便開發(fā)
Multivalued sections(增加兄纺,刪除大溜,移動行)
任何的XLFormSectionDescriptor對象都支持增加,刪除估脆,移動行數據钦奋,模式是可以配置的首先先是建立一組數據API:
section = [XLFormSectionDescriptor formSectionWithTitle:@"Multivalued TextField"
sectionOptions: XLFormSectionOptionCanReorder|XLFormSectionOptionCanInsert | XLFormSectionOptionCanDelete
sectionInsertMode:XLFormSectionInsertModeButton];
如上面所示,第二個參數可以根據配置得到支持任何增刪改的組合形式
第三個參數為增加的行數據進入界面的方式,XLFormSectionInsertModeLastRow (從右向左滑動進入) XLFormSectionInsertModeButton(從上到下進入) 第一個為默認參數
動態(tài)添加例子
- (id)init
{
XLFormDescriptor * form; //建立表單锨苏,等同于創(chuàng)建uitableview
XLFormSectionDescriptor * section;//建立組 section
XLFormRowDescriptor * row;//建立行相當于cell
//動態(tài)的將要添加一下行添加到組里
NSArray * nameList =@[@"family",@"male",@"female",@"client"];
form = [XLFormDescriptorformDescriptorWithTitle:@"Multivalued examples"];
// 初始化每個cell的類型與屬性
section = [XLFormSectionDescriptorformSectionWithTitle:@"MultiValued TextField"
sectionOptions:XLFormSectionOptionCanReorder | XLFormSectionOptionCanInsert |XLFormSectionOptionCanDelete];
section.multivaluedTag =@"textFieldRow";
[form addFormSection:section];
//添加循環(huán)遍歷數組動態(tài)添加數據 思考可以將數組做成plist文件疙教,這樣可以單獨配置有需求可以修改
for (NSString * tagin nameList) {
// 創(chuàng)建一個單行輸入框
row = [XLFormRowDescriptorformRowDescriptorWithTag:nilrowType:XLFormRowDescriptorTypeTexttitle:nil];
//設置placeholder
[[row cellConfig]setObject:@"Add a new tag"forKey:@"textField.placeholder"];
//添加默認值
row.value = [tagcopy];
//添加到一組中
[section addFormRow:row];
}
// 最后添加看一個添加按鈕
row = [XLFormRowDescriptorformRowDescriptorWithTag:nilrowType:XLFormRowDescriptorTypeTexttitle:nil];
[[row cellConfig]setObject:@"Add tag"forKey:@"textField.placeholder"];
[section addFormRow:row];
return [superinitWithForm:form];
}
獲取值
你能在-(NSDictionary *)formValues中獲取所有的行信息,或者是XLFormViewController實例和XLFormDescriptor實例
XLForm添加值添加在行信息XLFormRowDescriptor中伞租,它屬于XLFormSectionDescriptor贞谓,用建立XLFormRowDescriptor時傳入的Tag還獲取值,其中并不存multivaluedTag葵诈。
每一個組信息中都存在著一個multivaluedTag值裸弦,里面存在著當前組內的行信息的數組multivaluedTag值為Key
例如:
//根據 section.multivaluedTag = @"textFieldRow" 獲取到相應的組信息,打印出來的為當前組的所有行的value
NSArray * tempvalues =form.formValues[@"textFieldRow"];
NSLog(@"%@",form.formValues[@"textFieldRow"]);
//formSections中存儲著表單中所有組的指針
XLFormSectionDescriptor * tempsection=form.formSections[0];
//重新遍歷一次作喘,值是一樣的
for (int index=0; index