認(rèn)識(shí)XLForm

一 認(rèn)識(shí)

XLForm是創(chuàng)建動(dòng)態(tài)表格視圖最靈活的第三方庫(kù)侈贷,提供了各種cell樣式沪伙,比較簡(jiǎn)單實(shí)用液斜;

二 基本使用

  1. 創(chuàng)建控制器繼承自XLFormViewController

  2. 創(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];
    
  3. 表單賦值,不然表單不顯示

     self.form = form;
    

三 修改基本屬性

  1. 修改文字顏色境氢,字體大小

    以XLFormRowDescriptorTypeText類型的cell為例蟀拷,默認(rèn)創(chuàng)建出來(lái)是這樣的,

     row = [XLFormRowDescriptor formRowDescriptorWithTag:kBuildName rowType:XLFormRowDescriptorTypeText title:@"樓棟名稱"];
    
01.png

假如要修改文字大小和顏色的話萍聊,需要這樣做:

02.png
  1. 文本對(duì)齊方式
    XLForm中Text類型中的TextField文字默認(rèn)是左對(duì)齊的问芬,要是想滿足右對(duì)齊的話,需要這樣設(shè)置:

     [row.cellConfigAtConfigure setObject:@(NSTextAlignmentRight) forKey:@"textField.textAlignment"];
    
  2. 添加cell的accessoryView

    [row.cellConfig setObject:@(UITableViewCellAccessoryDisclosureIndicator) forKey:@"accessoryType"];
    

那么得到的效果如下:(右邊就會(huì)有一個(gè)小箭頭)


03.png
  1. 其它類型的cell
    1. 數(shù)字類型XLFormRowDescriptorTypeInteger寿桨,會(huì)調(diào)起數(shù)字鍵盤:


      04.png
    2. XLFormRowDescriptorTypeBooleanSwitch,


      05.png
    3. 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"];
      
06.png
  1. 日期類型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了骑歹,例如:

07.png

XLForm中提供了選擇器,但是一般情況下都是一個(gè)cell中只有一個(gè)cell墨微,但是我們的需求是一個(gè)cell中展示兩個(gè)選擇器來(lái)選擇道媚,所以我就整理下,自定義這個(gè)cell的整體思路:

  1. 首先我創(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,

09.png
08.png

(然后會(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)

  1. 改變cell中左邊Label和右邊Label的值
10.png

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末探颈,一起剝皮案震驚了整個(gè)濱河市熟丸,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌伪节,老刑警劉巖光羞,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異怀大,居然都是意外死亡纱兑,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門化借,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)潜慎,“玉大人,你說(shuō)我怎么就攤上這事蓖康☆盱牛” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵蒜焊,是天一觀的道長(zhǎng)倒信。 經(jīng)常有香客問(wèn)我,道長(zhǎng)泳梆,這世上最難降的妖魔是什么鳖悠? 我笑而不...
    開封第一講書人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任榜掌,我火速辦了婚禮,結(jié)果婚禮上乘综,老公的妹妹穿的比我還像新娘憎账。我一直安慰自己,他們只是感情好瘾带,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開白布鼠哥。 她就那樣靜靜地躺著熟菲,像睡著了一般看政。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上抄罕,一...
    開封第一講書人閱讀 51,573評(píng)論 1 305
  • 那天允蚣,我揣著相機(jī)與錄音,去河邊找鬼呆贿。 笑死嚷兔,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的做入。 我是一名探鬼主播冒晰,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼竟块!你這毒婦竟也來(lái)了壶运?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤浪秘,失蹤者是張志新(化名)和其女友劉穎蒋情,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體耸携,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡棵癣,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了夺衍。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狈谊。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖沟沙,靈堂內(nèi)的尸體忽然破棺而出河劝,到底是詐尸還是另有隱情,我是刑警寧澤尝胆,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布丧裁,位于F島的核電站,受9級(jí)特大地震影響含衔,放射性物質(zhì)發(fā)生泄漏煎娇。R本人自食惡果不足惜二庵,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望缓呛。 院中可真熱鬧催享,春花似錦、人聲如沸哟绊。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)票髓。三九已至攀涵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間洽沟,已是汗流浹背以故。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留裆操,地道東北人怒详。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像踪区,于是被迫代替她去往敵國(guó)和親昆烁。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 廢話不多說(shuō)缎岗,直接上干貨 ---------------------------------------------...
    小小趙紙農(nóng)閱讀 3,357評(píng)論 0 15
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多静尼,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,486評(píng)論 1 14
  • 自己到現(xiàn)在畢業(yè)一年密强,總結(jié)了自己在前段時(shí)間開發(fā)當(dāng)中遇到的的一些細(xì)節(jié)問(wèn)題茅郎,水平有限,希望有可以幫助大家的 1或渤,在OC中...
    baixuancheng閱讀 655評(píng)論 0 1
  • 有時(shí)候系冗,驀然之間,就是想開始一段簡(jiǎn)簡(jiǎn)單單的旅行薪鹦,不用多遠(yuǎn)掌敬,也不用多久,一個(gè)人池磁,一只包奔害,一條路,一段旅程地熄,一種心情华临,...
    夢(mèng)夢(mèng)_ada6閱讀 276評(píng)論 0 0
  • 目標(biāo)好遠(yuǎn) 什么才是個(gè)頭? 每個(gè)男人基本上都會(huì)有自己的夢(mèng)想 大部分男生的夢(mèng)想都會(huì)差不多 買車肯定是必須的 相對(duì)于我們...
    小渣渣閱讀 298評(píng)論 0 1