iOS开发之XLForm的使用
在iOS开发中,开发"表单"
界面,字段稍微多一点的一般都用UITableView来做,而XLForm
就是这样一个框架,它是创建动态表格视图最牛逼的iOS库, 用它实现表单功能,非常简单,省心省力。但是很可惜,搜索了很多文章都只是翻译官方文档,很多人在使用该库的时候可能都被官方文档带走远了,不知道如何具体使用。正好最近也要用到这个库,所以写个入门使用文章供大家参考。
一、 导入项目
使用CocoaPods或者手动导入库文件,本人选择直接导入项目源文件的方式。
二、改造表单ViewController
让ViewController
继承自XLFormViewController
,并重写下面的两个方法
@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
三、构造表单
- (void)initializeForm {
// 设置是否显示Cell之间分界线
//self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 设置Section的高度
self.tableView.sectionHeaderHeight = 30;
XLFormDescriptor * form;//form,一个表单只有一个
XLFormSectionDescriptor * section;//section,一个表单可能有多个
XLFormRowDescriptor * row; //row,每个section可能有多个row
// Form
form = [XLFormDescriptor formDescriptor];
// First section
section = [XLFormSectionDescriptor formSection];
section.title = @"用户";
[form addFormSection:section];
// 普通文本
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"username" rowType:XLFormRowDescriptorTypeText];
// 设置placeholder
[row.cellConfig setObject:@"用户名" forKey:@"textField.placeholder"];
// 设置文本颜色
[row.cellConfig setObject:[UIColor redColor] forKey:@"textField.textColor"];
[section addFormRow:row];
// 密码
row = [XLFormRowDescriptor formRowDescriptorWithTag:@"password" rowType:XLFormRowDescriptorTypePassword];
// 设置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 = @"暂无";
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];
// 开关
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{
// 判断是不是点击了确定按钮
if([formRow.tag isEqualToString:@"conform"] && formRow.rowType == XLFormRowDescriptorTypeButton){
//获取表单所有到的值
NSDictionary *values = [self formValues];
NSLog(@"%@", values);
}
[super didSelectFormRow:formRow];
}
//重写改该方法 上面的方法就不会调用了
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
// NSLog(@"%s", __func__);
//
//}
@end
四、效果图
五、总结
前面两步是官方文档中可以找到的,也很简单,关键在于initializeForm
方法中具体构造表单的过程,这里有必要强调几点:
-
XLFormViewController
实现了UITableViewDataSource, UITableViewDelegate
,并且持有一个UITableView
,这个从该类的声明可以看出来,所以UITableView 、UITableViewDataSource, UITableViewDelegate
中的方法都可以正常使用。
@interface XLFormViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, XLFormDescriptorDelegate, UITextFieldDelegate, UITextViewDelegate, XLFormViewControllerDelegate>
-
XLForm
将表单抽象为Form
,Section
,Row
三个层次,分别对应三个类
XLFormDescriptor * form;//form,一个表单只有一个
XLFormSectionDescriptor * section;//section,一个表单可能有多个
XLFormRowDescriptor * row; //row,每个section可能有多个row
每个表单中的具体信息最后都落脚到
XLFormRowDescriptor
中,通过它可以配置不同样式的表单项,通过构造函数的rowType
指定具体的表单类型,该框架提供了非常丰富的rowType
,具体可以参考官方文档说明。更细化配置表单项就需要借助于
XLFormRowDescriptor
中的属性进行配置,常用的有
@property (nonatomic, readonly, nonnull) NSMutableDictionary * cellConfig;
@property (nonatomic, readonly, nonnull) NSMutableDictionary * cellConfigForSelector;
这个配置的时候,往往有同学不知道具体如何才能设置属性,比如怎么设置表单输入框的placeholder
?更进一步如何设置placeholder
的颜色。其实它用到了KVC
,因为它们两个都是UITextField
类中的属性,那么直接进入UITextField
查找,发现如下信息:
@property(nullable, nonatomic,copy) NSString *placeholder;
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0);
那么设置起来就是
[row.cellConfig setObject:@"用户名" forKey:@"textField.placeholder"];
[row.cellConfig setObject:attrString forKey:@"textField.attributedPlaceholder"];
注意这里的key的写法,就是KVC
的写法。其他的属性依此类推。
- 如何获取设置好的表单的值?其实非常简单,该框架提供一个方法
formValues
,它的返回类型是一个NSDictionary
,其中key
就是XLFormRowDescriptor
设置时的Tag
。可以直接在控制器中调用该方法获取表单值,上面的效果图设置后的表单信息如下:
表单信息.png

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
界面无小事(二): 让RecyclerView展示更多不同视图
界面无小事(一): RecyclerView+CardView了解一下界面无小事(二): 让RecyclerView展示更多不同视图界面无小事(三):用RecyclerView + Toolbar做个文件选择器界面无小事(四):来写个滚动选择器吧!界面无小事(五):自定义TextView界面无小事(六):来做个好看得侧拉菜单! 目录 前言 GridLayoutManager的使用 Glide加载图片 让RecyclerView支持更多不同布局 来看看横向滚动 还有瀑布流 最后 前言 之前设置布局的时候用了最简单的LinearLayoutManager, 而且是单一布局, 这次来感受下GridLayoutManager和瀑布流以及多布局. GridLayoutManager的使用 比起LinearLayoutManager, GridLayoutManager可以适用的场景就更多了. 来看一段代码: RecyclerView rvTest = (RecyclerView) findViewById(R.id.rv_test); //rvTest.setLayoutManager(new ...
-
下一篇
Android dispatchTouchEvent检测多点触摸事件是否落入在某一个View区域内
Android dispatchTouchEvent检测多点触摸事件是否落入在某一个View区域内 检测发生在Android设备屏幕上到的点击事件是否发生在某一个View的区域内,重点方法是touchEventInView,该方法进行具体的检测,如果返回true,则表明该事件是发生在当前的参数view范围内,返回false则不是。 利用dispatchTouchEvent传出发生在全部区域上的点击事件,然后用touchEventInView计算判断。 dispatchTouchEvent返回true,则表明该事件消费掉了,不需要再次进行事件传递。换句话说,如果dispatchTouchEvent返回true,相当于对触摸事件进行了拦截处理。如果不想拦截不做任何处理,切忌不要简单粗暴的返回false,返回false导致意想不到的结果,而是要在dispatchTouchEvent里面利用super进行返回: return super.dispatchTouchEvent(event); 例: package zhangphil.test; import android.os.Bundle;...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- MySQL数据库在高并发下的优化方案