最近公司要做新项目,趁着现在不忙赶紧将一些知识点提前储备下,由于产品是一款安全产品,需要输入好多信息而且产品的需求又非常细,所以想讲页面做成那种可以配置的,期初简直是让我蛋都碎了,不过还好有强大的github,找到了这个XLForm,它可以方便的进行配置界面省时省力,所以将了解到的东西赶紧记下来,以后好方便开发
- Multivalued sections(增加,删除,移动行)
任何的XLFormSectionDescriptor对象都支持增加,删除,移动行数据,模式是可以配置的首先先是建立一组数据API:
section = [XLFormSectionDescriptor formSectionWithTitle:@"Multivalued TextField"
sectionOptions: XLFormSectionOptionCanReorder|XLFormSectionOptionCanInsert | XLFormSectionOptionCanDelete
sectionInsertMode:XLFormSectionInsertModeButton];
如上面所示,第二个参数可以根据配置得到支持任何增删改的组合形式
第三个参数为增加的行数据进入界面的方式,
XLFormSectionInsertModeLastRow
(从右向左滑动进入) XLFormSectionInsertModeButton(从上到下进入)
第一个为默认参数
动态添加例子
- (id)init
{
XLFormDescriptor * form; //建立表单,等同于创建uitableview
XLFormSectionDescriptor * section;//建立组 section
XLFormRowDescriptor * row;//建立行相当于cell
//动态的将要添加一下行添加到组里
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];
//添加循环遍历数组动态添加数据 思考可以将数组做成plist文件,这样可以单独配置有需求可以修改
for (NSString * tagin nameList) {
// 创建一个单行输入框
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<tempvalues.count; index++) {
XLFormRowDescriptor * row = tempsection.formRows[index];
NSLog(@"%@--%@",tempvalues[index],row.value);
}
综合案例:
XLFormDescriptor * form;//建立表单,等同于创建uitableview
XLFormSectionDescriptor * section;//建立组 section
XLFormRowDescriptor * row;//建立行相当于cell
//设置标题
form = [XLFormDescriptor formDescriptorWithTitle:@"Add Event"];
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// Title 设置textField
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"Title" rowType:XLFormRowDescriptorTypeText];
//设置placeholder
[row.cellConfigAtConfigure setObject:@"Title" forKey:@"textField.placeholder"];
row.required = YES;
[section addFormRow:row];
// Location
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"location" rowType:XLFormRowDescriptorTypeText];
[row.cellConfigAtConfigure setObject:@"Location" forKey:@"textField.placeholder"];
[section addFormRow:row];
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// All-day
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"all-day" rowType:XLFormRowDescriptorTypeBooleanSwitch title:@"All-day"];
[section addFormRow:row];
// Starts
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"starts" rowType:XLFormRowDescriptorTypeDateTimeInline title:@"Starts"];
row.value = [NSDate dateWithTimeIntervalSinceNow:60*60*24];
[section addFormRow:row];
// Ends
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"ends" rowType:XLFormRowDescriptorTypeDateTimeInline title:@"Ends"];
row.value = [NSDate dateWithTimeIntervalSinceNow:60*60*25];
[section addFormRow:row];
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// Repeat
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"repeat" rowType:XLFormRowDescriptorTypeSelectorPush title:@"Repeat"];
row.value = [XLFormOptionsObject formOptionsObjectWithValue:@(0) displayText:@"Never"];
row.selectorTitle = @"Repeat";
row.selectorOptions = @[[XLFormOptionsObject formOptionsObjectWithValue:@(0) displayText:@"Never"],
[XLFormOptionsObject formOptionsObjectWithValue:@(1) displayText:@"Every Day"],
[XLFormOptionsObject formOptionsObjectWithValue:@(2) displayText:@"Every Week"],
[XLFormOptionsObject formOptionsObjectWithValue:@(3) displayText:@"Every 2 Weeks"],
[XLFormOptionsObject formOptionsObjectWithValue:@(4) displayText:@"Every Month"],
[XLFormOptionsObject formOptionsObjectWithValue:@(5) displayText:@"Every Year"],
];
[section addFormRow:row];
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// Alert
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"alert" rowType:XLFormRowDescriptorTypeSelectorPush title:@"Alert"];
row.value = [XLFormOptionsObject formOptionsObjectWithValue:@(0) displayText:@"None"];
row.selectorTitle = @"Event Alert";
row.selectorOptions = @[[XLFormOptionsObject formOptionsObjectWithValue:@(0) displayText:@"None"],
[XLFormOptionsObject formOptionsObjectWithValue:@(1) displayText:@"At time of event"],
[XLFormOptionsObject formOptionsObjectWithValue:@(2) displayText:@"5 minutes before"],
[XLFormOptionsObject formOptionsObjectWithValue:@(3) displayText:@"15 minutes before"],
[XLFormOptionsObject formOptionsObjectWithValue:@(4) displayText:@"30 minutes before"],
[XLFormOptionsObject formOptionsObjectWithValue:@(5) displayText:@"1 hour before"],
[XLFormOptionsObject formOptionsObjectWithValue:@(6) displayText:@"2 hours before"],
[XLFormOptionsObject formOptionsObjectWithValue:@(7) displayText:@"1 day before"],
[XLFormOptionsObject formOptionsObjectWithValue:@(8) displayText:@"2 days before"],
];
[section addFormRow:row];
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// Show As
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"showAs" rowType:XLFormRowDescriptorTypeSelectorPush title:@"Show As"];
row.value = [XLFormOptionsObject formOptionsObjectWithValue:@(0) displayText:@"Busy"];
row.selectorTitle = @"Show As";
row.selectorOptions = @[[XLFormOptionsObject formOptionsObjectWithValue:@(0) displayText:@"Busy"],
[XLFormOptionsObject formOptionsObjectWithValue:@(1) displayText:@"Free"]];
[section addFormRow:row];
section = [XLFormSectionDescriptor formSection];
[form addFormSection:section];
// URL
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"url" rowType:XLFormRowDescriptorTypeURL];
[row.cellConfigAtConfigure setObject:@"URL" forKey:@"textField.placeholder"];
[section addFormRow:row];
// Notes
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"notes" rowType:XLFormRowDescriptorTypeTextView];
[row.cellConfigAtConfigure setObject:@"Notes" forKey:@"textView.placeholder"];
[section addFormRow:row];
self.form = form;
文字改变监听:
#pragma mark - 文字修改代理
-(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
{
NSLog(@"rowDescriptor.tag :%@",rowDescriptor.tag);
NSLog(@"rowDescriptor.value:%@",rowDescriptor.value);
NSLog(@"oldValue:%@",oldValue);
NSLog(@"newValue:%@",newValue);
[super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
if ([rowDescriptor.tag isEqualToString:@"alert"]){
if ([[rowDescriptor.value valueData] isEqualToNumber:@(0)] == NO && [[oldValue valueData] isEqualToNumber:@(0)]){
// 动态添加
XLFormRowDescriptor * newRow = [rowDescriptor copy];
newRow.tag = @"secondAlert";
newRow.title = @"Second Alert";
[self.form addFormRow:newRow afterRow:rowDescriptor];
}
else if ([[oldValue valueData] isEqualToNumber:@(0)] == NO && [[newValue valueData] isEqualToNumber:@(0)]){
[self.form removeFormRowWithTag:@"secondAlert"];
}
}
else if ([rowDescriptor.tag isEqualToString:@"all-day"]){
XLFormRowDescriptor * startDateDescriptor = [self.form formRowWithTag:@"starts"];
XLFormRowDescriptor * endDateDescriptor = [self.form formRowWithTag:@"ends"];
XLFormDateCell * dateStartCell = (XLFormDateCell *)[[self.form formRowWithTag:@"starts"] cellForFormController:self];
XLFormDateCell * dateEndCell = (XLFormDateCell *)[[self.form formRowWithTag:@"ends"] cellForFormController:self];
if ([[rowDescriptor.value valueData] boolValue] == YES){
startDateDescriptor.valueTransformer = [DateValueTrasformer class];
endDateDescriptor.valueTransformer = [DateValueTrasformer class];
[dateStartCell setFormDatePickerMode:XLFormDateDatePickerModeDate];
[dateEndCell setFormDatePickerMode:XLFormDateDatePickerModeDate];
}
else{
startDateDescriptor.valueTransformer = [DateTimeValueTrasformer class];
endDateDescriptor.valueTransformer = [DateTimeValueTrasformer class];
[dateStartCell setFormDatePickerMode:XLFormDateDatePickerModeDateTime];
[dateEndCell setFormDatePickerMode:XLFormDateDatePickerModeDateTime];
}
[self updateFormRow:startDateDescriptor];
[self updateFormRow:endDateDescriptor];
}
else if ([rowDescriptor.tag isEqualToString:@"starts"]){
XLFormRowDescriptor * startDateDescriptor = [self.form formRowWithTag:@"starts"];
XLFormRowDescriptor * endDateDescriptor = [self.form formRowWithTag:@"ends"];
if ([startDateDescriptor.value compare:endDateDescriptor.value] == NSOrderedDescending) {
// startDateDescriptor is later than endDateDescriptor
endDateDescriptor.value = [[NSDate alloc] initWithTimeInterval:(60*60*24) sinceDate:startDateDescriptor.value];
[endDateDescriptor.cellConfig removeObjectForKey:@"detailTextLabel.attributedText"];
[self updateFormRow:endDateDescriptor];
}
}
else if ([rowDescriptor.tag isEqualToString:@"ends"]){
XLFormRowDescriptor * startDateDescriptor = [self.form formRowWithTag:@"starts"];
XLFormRowDescriptor * endDateDescriptor = [self.form formRowWithTag:@"ends"];
XLFormDateCell * dateEndCell = (XLFormDateCell *)[endDateDescriptor cellForFormController:self];
if ([startDateDescriptor.value compare:endDateDescriptor.value] == NSOrderedDescending) {
// startDateDescriptor is later than endDateDescriptor
[dateEndCell update]; // force detailTextLabel update
NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:@1
forKey:NSStrikethroughStyleAttributeName];
NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:dateEndCell.detailTextLabel.text attributes:strikeThroughAttribute];
[endDateDescriptor.cellConfig setObject:strikeThroughText forKey:@"detailTextLabel.attributedText"];
[self updateFormRow:endDateDescriptor];
}
else{
[endDateDescriptor.cellConfig removeObjectForKey:@"detailTextLabel.attributedText"];
[self updateFormRow:endDateDescriptor];
}
}
}