iOS实现自定义表单实例代码

2020-01-21 05:27:44刘景俊

这样,表单数据就独立于UI,TableViewCell是可配置的。通过XLFormRowDescriptor 来配置TableViewCell。只要指定行类型,就给出相应的类型的Cell。TableViewController中的Cell是由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 class]], @"UITableViewCell must extend from WWEBaseTableViewCell");
 _tableViewCell.rowDescriptor = self;
 }
 return _tableViewCell;
}

那么 XLFormRowDescriptor 怎么根据不同的行给出不同的cell?cell需要继承同一个父类,这个父类足够简单,只有一个WWEFormRowDescriptor属性,其它需要实现的方法则由WWECellProtocal负责。


@class WWEFormRowDescriptor;
@interface WWEBaseTableViewCell : UITableViewCell<WWECellProtocal>
@property (nonatomic, weak) WWEFormRowDescriptor *rowDescriptor;
@end

@class WWEFormRowDescriptor;
@protocol WWECellProtocal <NSObject>
@required
@property (nonatomic, weak) WWEFormRowDescriptor *rowDescriptor;
-(void)configure;
-(void)update;
@end

另外,将表单配置数据独立出来,而不是写死在代码中。使用plist文件,这样初始化form时只需读取这个文件,就完成了cell的配置。后续如果要改动表单的内容,不改动样式,就只需修改plist文件,而无需改动代码。

ios,表单,实现表单提交form,自定义表单

最后TableViewController的代码就格外简单了:

ios,表单,实现表单提交form,自定义表单

这样上传表单数据就无比轻松了, [self.form localValidationErrors] 验证数据合法性,全部合法的话再调用 [self.form httpParameters] 就获取了所有的表单数据,传给网络接口就大功告成了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对ASPKU的支持。


注:相关教程知识阅读请移步到IOS开发频道。