一 認識
XLForm是創建動態表格視圖最靈活的第三方庫,提供了各種cell樣式,比較簡單實用;
二 基本使用
創建控制器繼承自XLFormViewController
-
創建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為例,默認創建出來是這樣的,
row = [XLFormRowDescriptor formRowDescriptorWithTag:kBuildName rowType:XLFormRowDescriptorTypeText title:@"樓棟名稱"];
假如要修改文字大小和顏色的話,需要這樣做:
-
文本對齊方式
XLForm中Text類型中的TextField文字默認是左對齊的,要是想滿足右對齊的話,需要這樣設置:[row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
-
添加cell的accessoryView
[row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
那么得到的效果如下:(右邊就會有一個小箭頭)
- 其它類型的cell
-
數字類型XLFormRowDescriptorTypeInteger,會調起數字鍵盤:
04.png -
XLFormRowDescriptorTypeBooleanSwitch,
05.png -
pickerView類型XLFormRowDescriptorTypeSelectorPickerView,
row = [XLFormRowDescriptor formRowDescriptorWithTag:kBuildConstruction rowType:XLFormRowDescriptorTypeSelectorPickerView title:@"建筑結構"]; row.required = YES; row.selectorOptions =@[[XLFormOptionsObject formOptionsObjectWithValue:@"建筑結構一" displayText: @"建筑結構一"], [XLFormOptionsObject formOptionsObjectWithValue:@"建筑結構二" displayText: @"建筑結構二"] ]; row.value = [XLFormOptionsObject formOptionsObjectWithValue:@"建筑結構一" displayText:@"建筑結構一"]; [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,但是在實際開發中可能并不是非常滿足自己的需求,這時候就需要我們去自定義cell了,例如:
XLForm中提供了選擇器,但是一般情況下都是一個cell中只有一個cell,但是我們的需求是一個cell中展示兩個選擇器來選擇,所以我就整理下,自定義這個cell的整體思路:
- 首先我創建了一個cell叫做XLFormTwoSelectorCell繼承自XLFormBaseCell,然后發現,必須實現3個方法:
-
load()注冊自定義的cell;
+(void)load{}
-
configure()配置一些基本cell信息
-(void)configure{}
-
update() 更新tableView中顯示的值
-(void)update{}
2.然后我查看了一個cell中只有一個selector的XLFormSelectorCell,是XLForm中的源代碼,發現不管是一個selector還是兩個selector都只是在點擊cell某個位置彈出pickerView而已,所以首先我重寫了成為響應者的方法:
我給cell左半部分和有半部分分別加了手勢,點擊時候成為第一響應者,彈出pickerView,
(然后會發現,因為是一個pickerView,所以點右邊或者是點擊左邊每次pickerView并不能對應到當前的row,所以我就子安點擊的時候讓pickerView滑動到對應的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;
//記錄點擊左邊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]));
}
//點擊cell的時候不讓彈出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;
}
//點擊完成
-(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