这篇文章给大家分享的是有关iOS如何实现自定义表单的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
<强>前言强>
最近在开发一个应用,需要让用户填写数据,然后上传到服务端进行计算并返回结果在客户端中展示。其中需要填写的数据项多达十几项,大部分是必填。所有表单数据在一个页面中实现,在应用程序中这样的设计其实挺逆天的,但产品经理坚持要这么弄,也只能硬着头皮写。页面的表单数据样式五花八门,下图是其中几行截图
第一、二行的textfield其实是一个选择框,只能从下拉选项中选择一个。第三个只允许输入数字。
页面由另一个同学实现,表单的数据基本都在cellForRowAtIndexPath实现,结果是这样的:
最主要的是红色方框的三个类:XLFormRowDescriptor, XLFormSectionDescriptor, XLFormDescriptor.XLFormDescriptor结构和UITablView一样,有部分,有行,它就是为成为UITableView的数据源而设计的.XLFormRowDescriptor定义了每行表单的数据内容,包括行样式,标题,行类型,选择项内容,标签,合法性验证等.XLFormSectionDescriptor是由XLFormRowDescriptor组合而成的,而XLFormSectionDescriptor最终又组成了XLFormDescriptor。
由于我们要实现的程序表单行样式更复杂,有的一行要提交两项数据,所以需要对XLFormRowDescriptor做些改动。代码如下:
@property (强,,原子),NSMutableDictionary * cellConfig; @property (强,,原子),NSDictionary * textFieldConfig; @property (只读,那么强大,还以为;原子),NSString * rowType; @property (只读,那么强大,还以为;原子),NSArray * leftOptions; @property (只读,那么强大,还以为;原子),WWEFormRightSelectorOption * rightOptions; @property (强,,原子),NSString *标题; @property (强,,原子),id 价值; @property (强,,原子),NSString *标记; @property (原子),BOOL 需要; @property (强,,原子),WWEBaseTableViewCell * tableViewCell; - (id) initWithRowType:(NSString *) rowType 标题:才能(NSString *)标题 leftOptions才能:(NSArray *) leftOptions ,rightOptions:(WWEFormRightSelectorOption *) rightOptions; ——(WWEFormValidation *) doValidation; @end @interface WWEFormRightSelectorOption : NSObject @property (只读的,原子),NSArray * rightOptions; @property (只读的,原子),NSString * httpParameterKey; @property (只读的,原子),NSString * selectorTitle; @property (原子),NSInteger selectedIndex; + (WWEFormRightSelectorOption *) formRightSelectorOptionWithTitle:(NSString *) title ,,,,, httpParameterKey: (NSString *) httpParameterKey ,,,,, rightOptions: (NSArray *) rightOptions;
这样,表单数据就独立于UI, TableViewCell是可配置的。通过XLFormRowDescriptor来配置TableViewCell。只要指定行类型,就给出相应的类型的Cell.TableViewController中的细胞是由XLFormRowDescriptor提供的。
安康;(WWEBaseTableViewCell *) tableViewCell { ,if (! _tableViewCell), { ,id cellClass =, [self cellClassesForRowDescriptorTypes] [self.rowType]; ,NSAssert (cellClass, @" Not defined XLFormRowDescriptorType:, % @",, self.rowType ?:, @"“); ,=,_tableViewCell [[cellClass alloc], initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil); , ,NSAssert ([_tableViewCell isKindOfClass: [WWEBaseTableViewCell 类]],,@" UITableViewCell must extend 得到WWEBaseTableViewCell");=,_tableViewCell.rowDescriptor 自我; ,} ,return _tableViewCell; }iOS如何实现自定义表单