iOS XLFrom 使用

XLFrom 使用简介

程序员是如何通过造轮子走向人生巅峰的?

image.png

一、引入

  1. 开发中经常遇到如下实例:

    如果我们需要开发这样的一个页面的话,我们一定能想到使用tableview就完全足够使用,那么就需要在tableview代理中,进行多个样式的判断。就需要写比较多if-else这样的代码。

aa.png
  1. 解决方案

    XLForm 是最灵活且最强大的创建动态表单的iOS库。点击这里查看项目地址

    XLForm提供了用于创建表单的功能非常强大。它会在运行时跟踪并动态更新UI。

二、使用

  1. 集成到项目中
    pod 'XLForm'
  1. 创建继承自XLFormViewController的控制器
image.png
  1. 在控制器中创建表单

     -(void)initializeForm {
    
         XLFormDescriptor * form; //表单
    
     XLFormSectionDescriptor * section; //组
    
     XLFormRowDescriptor * row; //行
     
     form = [XLFormDescriptor formDescriptor]; //初始化
    
     section = [XLFormSectionDescriptor formSectionWithTitle:@"section"];
    
     [form addFormSection:section];
     
         row = [XLFormRowDescriptor formRowDescriptorWithTag:@"row" rowType:XLFormRowDescriptorTypeBooleanCheck title:@"选中样式:"];
    
     [section addFormRow:row];
    
     row = [XLFormRowDescriptor formRowDescriptorWithTag:@"row2" rowType:XLFormRowDescriptorTypeSlider title:@"滑动条样式:"];
    
     [section addFormRow:row];
    
     self.form = form;
    
     }
    

    运行结果:

    image.png
  2. 基本类的说明

    创建一个表单,主要用到了三个类:

    • XLFormRowDescriptor
    • XLFormSectionDescriptor
    • XLFormDescriptor

    由此我们发现,此结构就表现为: (Table -->> Sections -- >> Rows)。

    由此产生的表格视图的结构(sections and rows)反映了定义的结构

  3. tableView的创建时机

    在XLFormViewController中的ViewDidLoad方法中已经进行创建,并做一些初始化设置,所以我们在使用的时候为什么要继承自XLFormViewController,这里就体现出来了。

     -(void)viewDidLoad {
     [super viewDidLoad];
    
     UITableView *tableView = self.tableView;
     if (!tableView){
         tableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                               style:self.tableViewStyle];
         tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
         if([tableView respondsToSelector:@selector(cellLayoutMarginsFollowReadableWidth)]) {
             tableView.cellLayoutMarginsFollowReadableWidth = NO;
             }
     }
     if (!tableView.superview){
         [self.view addSubview:tableView];
         self.tableView = tableView;
          }
     if (!self.tableView.delegate){
         self.tableView.delegate = self;
     }
     if (!self.tableView.dataSource){
         self.tableView.dataSource = self;
     }
     if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
         self.tableView.rowHeight = UITableViewAutomaticDimension;
         self.tableView.estimatedRowHeight = 44.0;
         }
     if (self.form.title){
         self.title = self.form.title;
     }
     [self.tableView setEditing:YES animated:NO];
     self.tableView.allowsSelectionDuringEditing = YES;
     self.form.delegate = self;
     _oldBottomTableContentInset = nil;
     }
    
  4. Cell 如何创建 (XLForViewController)

    image.png
    image.png

    XLFormRowDescriptor

    image.png
  1. Cell的样式定制 (采用KVC机制)

    实现方式(XLFormViewCronller):

    image.png

    例如

     // 设置placeholder
    

    [row.cellConfig setObject:@"用户名" forKey:@"textField.placeholder"];
    // 设置文本颜色
    [row.cellConfig setObject:[UIColor redColor] forKey:@"textField.textColor"];

     // 密码
    

    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];

  1. 获取Cell上的值

     [self.form formValues]
    

    它的返回类型是一个NSDictionary,其中key就是XLFormRowDescriptor设置时的Tag。可以直接在控制器中调用该方法获取表单值

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容