一 認(rèn)識(shí)
XLForm是創(chuàng)建動(dòng)態(tài)表格視圖最靈活的第三方庫(kù)侈贷,提供了各種cell樣式沪伙,比較簡(jiǎn)單實(shí)用液斜;
二 基本使用
創(chuàng)建控制器繼承自XLFormViewController
-
創(chuàng)建form表單待秃,添加section(組)遗遵,添加row(行)
XLFormDescriptor *form = [XLFormDescriptor formDescriptor]; XLFormSectionDescriptor *section; XLFormRowDescriptor *row; section = [XLFormSectionDescriptor formSection]; [form addFormSection:section]; row = [XLFormRowDescriptor formRowDescriptorWithTag:kBuildName rowType:XLFormRowDescriptorTypeText title:@"樓棟名稱"]; row.required = YES; [self customizeFormRowFont:row]; [section addFormRow:row];
-
表單賦值,不然表單不顯示
self.form = form;
三 修改基本屬性
-
修改文字顏色境氢,字體大小
以XLFormRowDescriptorTypeText類型的cell為例蟀拷,默認(rèn)創(chuàng)建出來(lái)是這樣的,
row = [XLFormRowDescriptor formRowDescriptorWithTag:kBuildName rowType:XLFormRowDescriptorTypeText title:@"樓棟名稱"];
假如要修改文字大小和顏色的話萍聊,需要這樣做:
-
文本對(duì)齊方式
XLForm中Text類型中的TextField文字默認(rèn)是左對(duì)齊的问芬,要是想滿足右對(duì)齊的話,需要這樣設(shè)置:[row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
-
添加cell的accessoryView
[row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
那么得到的效果如下:(右邊就會(huì)有一個(gè)小箭頭)
- 其它類型的cell
-
數(shù)字類型XLFormRowDescriptorTypeInteger寿桨,會(huì)調(diào)起數(shù)字鍵盤:
-
XLFormRowDescriptorTypeBooleanSwitch,
-
pickerView類型XLFormRowDescriptorTypeSelectorPickerView此衅,
row = [XLFormRowDescriptor formRowDescriptorWithTag:kBuildConstruction rowType:XLFormRowDescriptorTypeSelectorPickerView title:@"建筑結(jié)構(gòu)"]; row.required = YES; row.selectorOptions =@[[XLFormOptionsObject formOptionsObjectWithValue:@"建筑結(jié)構(gòu)一" displayText: @"建筑結(jié)構(gòu)一"], [XLFormOptionsObject formOptionsObjectWithValue:@"建筑結(jié)構(gòu)二" displayText: @"建筑結(jié)構(gòu)二"] ]; row.value = [XLFormOptionsObject formOptionsObjectWithValue:@"建筑結(jié)構(gòu)一" displayText:@"建筑結(jié)構(gòu)一"]; [row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
-
-
日期類型XLFormRowDescriptorTypeDate强戴,
row = [XLFormRowDescriptor formRowDescriptorWithTag:kBuildTime rowType:XLFormRowDescriptorTypeDate title:@"建筑年份"];
row.value = [NSDate date];
[row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
四 自定義Cell
雖然XLForm中提供了很多種類型的cell,但是在實(shí)際開發(fā)中可能并不是非常滿足自己的需求挡鞍,這時(shí)候就需要我們?nèi)プ远xcell了骑歹,例如:
XLForm中提供了選擇器,但是一般情況下都是一個(gè)cell中只有一個(gè)cell墨微,但是我們的需求是一個(gè)cell中展示兩個(gè)選擇器來(lái)選擇道媚,所以我就整理下,自定義這個(gè)cell的整體思路:
- 首先我創(chuàng)建了一個(gè)cell叫做XLFormTwoSelectorCell繼承自XLFormBaseCell翘县,然后發(fā)現(xiàn)最域,必須實(shí)現(xiàn)3個(gè)方法:
-
load()注冊(cè)自定義的cell;
+(void)load{}
-
configure()配置一些基本cell信息
-(void)configure{}
-
update() 更新tableView中顯示的值
-(void)update{}
2.然后我查看了一個(gè)cell中只有一個(gè)selector的XLFormSelectorCell锈麸,是XLForm中的源代碼镀脂,發(fā)現(xiàn)不管是一個(gè)selector還是兩個(gè)selector都只是在點(diǎn)擊cell某個(gè)位置彈出pickerView而已,所以首先我重寫了成為響應(yīng)者的方法:
我給cell左半部分和有半部分分別加了手勢(shì)忘伞,點(diǎn)擊時(shí)候成為第一響應(yīng)者薄翅,彈出pickerView,
(然后會(huì)發(fā)現(xiàn)虑省,因?yàn)槭且粋€(gè)pickerView匿刮,所以點(diǎn)右邊或者是點(diǎn)擊左邊每次pickerView并不能對(duì)應(yīng)到當(dāng)前的row,所以我就子安點(diǎn)擊的時(shí)候讓pickerView滑動(dòng)到對(duì)應(yīng)的row)
- 改變cell中左邊Label和右邊Label的值
4.整體代碼:
#import "XLFormTwoSelectorCell.h"
NSString * const XLFormRowDescriptorCustomSelectorCell = @"XLFormRowDescriptorCustomSelectorCell";
@interface XLFormTwoSelectorCell() <UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *leftTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightTitleLabel;
@property (strong, nonatomic) UIPickerView * pickerView;
@property (weak, nonatomic) IBOutlet UIView *leftView;
@property (weak, nonatomic) IBOutlet UIView *rightView;
@property(assign, nonatomic) NSInteger leftIndex;
@property(assign, nonatomic) NSInteger rightIndex;
//記錄點(diǎn)擊左邊view還是右邊view
@property(assign, nonatomic) BOOL isClickRight;
@property(strong, nonatomic) NSMutableDictionary *value;
@end
@implementation XLFormTwoSelectorCell
-(NSMutableDictionary *)value {
if (!_value) {
_value = [NSMutableDictionary dictionary];
}
return _value;
}
-(UIPickerView *)pickerView
{
if (!_pickerView) {
_pickerView = [[UIPickerView alloc] init];
_pickerView.delegate = self;
_pickerView.dataSource = self;
[_pickerView selectRow:[self selectedIndex] inComponent:0 animated:NO];
}
return _pickerView;
}
-(UIView *)inputView {
if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorCustomSelectorCell]){
return self.pickerView;
}
return [super inputView];
}
-(void)awakeFromNib {
[super awakeFromNib];
UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapLeft)];
[self.leftView addGestureRecognizer:tapLeft];
UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRight)];
[self.rightView addGestureRecognizer:tapRight];
}
-(void)tapLeft {
self.isClickRight = NO;
[self.pickerView selectRow:self.leftIndex inComponent:0 animated:NO];
[self becomeFirstResponder];
}
-(void)tapRight {
self.isClickRight = YES;
[self.pickerView selectRow:self.rightIndex inComponent:0 animated:NO];
[self becomeFirstResponder];
}
-(BOOL)formDescriptorCellCanBecomeFirstResponder
{
return (!self.rowDescriptor.isDisabled && ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorCustomSelectorCell]));
}
//點(diǎn)擊cell的時(shí)候不讓彈出pickerView
-(BOOL)formDescriptorCellBecomeFirstResponder
{
return NO;
}
-(void)formDescriptorCellDidSelectedWithFormController: (XLFormViewController *)controller
{
if (self.rowDescriptor.action.formBlock){
self.rowDescriptor.action.formBlock(self.rowDescriptor);
}
if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorCustomSelectorCell]){
[controller.tableView selectRowAtIndexPath:nil animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
- (BOOL)canBecomeFirstResponder
{
if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorCustomSelectorCell]){
return YES;
}
return [super canBecomeFirstResponder];
}
+(void)load {
[XLFormViewController.cellClassesForRowDescriptorTypes setObject:NSStringFromClass([XLFormTwoSelectorCell class]) forKey:XLFormRowDescriptorCustomSelectorCell];
}
-(void)configure
{
[super configure];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
//點(diǎn)擊完成
-(void)update
{
[super update];
self.editingAccessoryType = self.accessoryType;
if (self.rowDescriptor.value) {
NSDictionary *dict = self.rowDescriptor.value;
self.leftTitleLabel.text = dict[@(0)];
self.rightTitleLabel.text = dict[@(1)];
}
}
-(NSString *)valueDisplayText {
if (self.rowDescriptor.selectorOptions) {
NSArray *array = self.rowDescriptor.selectorOptions[self.isClickRight][@"values"];
NSInteger row = [self.pickerView selectedRowInComponent:0];
return array[row];
}
return nil;
}
-(void)highlight
{
[super highlight];
if (self.isClickRight) {
self.rightTitleLabel.textColor = self.tintColor;
}else {
self.leftTitleLabel.textColor = self.tintColor;
}
}
-(void)unhighlight {
[super unhighlight];
if (self.isClickRight) {
self.rightTitleLabel.textColor = [UIColor colorWithHexString:@"#222222"];
}else {
self.leftTitleLabel.textColor = [UIColor colorWithHexString:@"#222222"];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (self.rowDescriptor.valueTransformer){
NSAssert([self.rowDescriptor.valueTransformer isSubclassOfClass:[NSValueTransformer class]], @"valueTransformer is not a subclass of NSValueTransformer");
NSValueTransformer * valueTransformer = [self.rowDescriptor.valueTransformer new];
NSString * tranformedValue = [valueTransformer transformedValue:[[self.rowDescriptor.selectorOptions objectAtIndex:row] valueData]];
if (tranformedValue){
return tranformedValue;
}
}
NSArray *array = self.rowDescriptor.selectorOptions[self.isClickRight][@"values"];
return array[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([self.rowDescriptor.rowType isEqualToString:XLFormRowDescriptorCustomSelectorCell]){
NSArray *array = self.rowDescriptor.selectorOptions[self.isClickRight][@"values"];
if (self.isClickRight) {
self.rightTitleLabel.text = array[row];
self.rightIndex = row;
self.value[@(1)] = array[row];
}else {
self.leftTitleLabel.text = array[row];
self.leftIndex = row;
self.value[@(0)] = array[row];
}
self.rowDescriptor.value = self.value;
[self setNeedsLayout];
}
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSArray *array = self.rowDescriptor.selectorOptions[self.isClickRight][@"values"];
return array.count;
}
#pragma mark - Helpers
-(NSInteger)selectedIndex
{
if (self.rowDescriptor.selectorOptions) {
NSArray *array = self.rowDescriptor.selectorOptions[self.isClickRight][@"values"];
return array.count;
}
return -1;
}
@end