前段时间封装了简易的自适应表单系统,但是未能一并封装表单编辑以及添加附件功能。今天正好有时间,便把之前遗落的编辑以及附件功能加上,希望能为大家提供思路。
github地址(欢迎Star!):https://github.com/RockChanel/SelAttachmentForm
Form.gif由于此版本封装较为仓促,最近针对此版本进行重构,去除了第三方的强依赖,可以根据需求引用,修复部分字段无效的问题,并将可定制化内容接口尽可能统一,详情请移步 iOS 高度封装自适应表单(重构版)
1. 表单条目类别
/** 表单cell类型 */
typedef NS_ENUM(NSInteger, SelwynFormCellType) {
SelwynFormCellTypeNone = 0, //default
SelwynFormCellTypeInput = 1, //单行或多行输入
SelwynFormCellTypeTextViewInput = 2, //包含textView输入
SelwynFormCellTypeSelect = 3, //选择
SelwynFormCellTypeAttachment = 4, //附件
};
2. 表单条目创建
/**
快捷创建新增表单条目
@param formTitle 标题
@param formDetail 详情
@param cellType cell类型
@param keyboardType 键盘类型
@param editable 是否可编辑
@param required 是否必填(必选)
@return 表单条目
*/
NS_INLINE SelwynFormItem *SelwynItemMake(NSString *formTitle, NSString *formDetail, SelwynFormCellType cellType, UIKeyboardType keyboardType, BOOL editable, BOOL required){
return [SelwynFormHandle itemWithFormTitle:formTitle formDetail:formDetail cellType:cellType keyboardType:keyboardType editable:editable required:required];
}
/**
快捷创建详情表单条目
@param formTitle 标题
@param formDetail 详情
@param cellType cell类型
@return 表单条目
*/
NS_INLINE SelwynFormItem *SelwynDetailItemMake(NSString *formTitle, NSString *formDetail, SelwynFormCellType cellType){
return [SelwynFormHandle detailItemWithFormTitle:formTitle formDetail:formDetail cellType:cellType];
}
SelwynFormCellTypeInput:
SelwynFormItem *phoneNumber = SelwynItemMake(@"手机号", @"", SelwynFormCellTypeInput, UIKeyboardTypeNumberPad, YES, YES);
phoneNumber.maxInputLength = 11; //添加字数限制
SelwynFormCellTypeTextViewInput:
SelwynFormItem *intro = SelwynItemMake(@"个人简介", @"", SelwynFormCellTypeTextViewInput, UIKeyboardTypeDefault, YES, NO);
SelwynFormCellTypeSelect:
SelwynFormItem *gender = SelwynItemMake(@"性别", @"", SelwynFormCellTypeSelect, UIKeyboardTypeDefault, NO, YES);
gender.selectHandle = ^(SelwynFormItem *item) {
};
SelwynFormCellTypeAttachment:
SelwynFormItem *attachment = SelwynItemMake(@"附件", @"", SelwynFormCellTypeAttachment, UIKeyboardTypeDefault, YES, NO);
3. 编辑
//编辑按钮点击事件
self.editCompletion = ^(){
[weakSelf.editBtn setTitle:@"完成" forState:UIControlStateNormal];
name.editable = YES; //重置可编辑属性
name.required = YES; //重置可选必选
[weakSelf.formTableView reloadData];
};
4. 表单内容校验(可根据需求制定)
校验单个条目内容:
/** 校验表单内容 */
+ (SelwynFormItem *)checkContentWithItem:(SelwynFormItem *)item{
item.formError = @"";
if (item.required) {
if (!item.formDetail || [item.formDetail isEqualToString:@""]) {
if (item.formCellType == SelwynFormCellTypeInput || item.formCellType == SelwynFormCellTypeTextViewInput) {
item.formError = [NSString stringWithFormat:@"请输入%@",item.formTitle];
}else if (item.formCellType == SelwynFormCellTypeSelect){
item.formError = [NSString stringWithFormat:@"请选择%@",item.formTitle];
}
}
}
return item;
}
校验整个表单:
/** 校验数据 */
- (void)commit{
for (int sec = 0; sec < self.mutableArray.count; sec++) {
SelwynFormSectionItem *sectionItem = self.mutableArray[sec];
for (int row = 0; row < sectionItem.cellItems.count; row++) {
SelwynFormItem *rowItem = sectionItem.cellItems[row];
SelwynFormItem *checkItem = [SelwynFormHandle checkContentWithItem:rowItem];
if (![checkItem.formError isEqualToString:@""]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:checkItem.formError message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
return;
}
}
}
}
通过快捷添加条目的方式构建表单系统,大幅度提升表单开发效率,希望此表单系统能够对大家有所帮助。
网友评论