iOS-XLFrom動(dòng)態(tài)獲設(shè)置Cell行高

之前一直在寫(xiě)XLFrom表單提交,行高一直是固定的,后來(lái)有個(gè)需求需要?jiǎng)討B(tài)變換行高,自己也是腦抽了用了XLFrom來(lái)寫(xiě),到這個(gè)界面快寫(xiě)完了,需求里面動(dòng)態(tài)獲取行高也是醉了。

XLFrom里面的行高設(shè)置是一個(gè)非實(shí)例方法

之前一直是固定的,所以在網(wǎng)上也沒(méi)有找到解決方法。

+(CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor{
  //返回的高度
    return 50.0;
}

自己研究了一下
在初始化表單的時(shí)候創(chuàng)建一個(gè)通知

- (instancetype)init
{
  self = [super init];
  if (self) {
      [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setFormCellHeight:) name:setFromCellHeightNSNotification object:nil];
      [self initializeForm];
  }
  return self;
}

然后在給section的時(shí)候,把數(shù)據(jù)傳過(guò)去
這里其實(shí)我是動(dòng)態(tài)創(chuàng)建了list長(zhǎng)度的section
每個(gè)cell高度都是不同的。

   NSArray *list = @[
  @[@"14棟101",@"14棟102",@"14棟103",@"14棟104"],
  @[@"15棟101",@"15棟102",@"15棟103",@"15棟104",@"15棟105",@"15棟106",@"15棟106",@"15棟106",@"15棟106"],
  @[@"16棟101",@"16棟102",@"16棟103",@"16棟104",@"16棟105",@"16棟106"]];
    NSArray *tag = @[@"list1",@"list2",@"list3"];
    NSArray *title = @[@"一樓",@"二樓",@"三樓"];
    for (int i = 0; i<list.count; i++) {
        section = [XLFormSectionDescriptor formSection];
        [form addFormSection:section];
        
        //所屬區(qū)域
        row = [XLFormRowDescriptor formRowDescriptorWithTag:tag[i] rowType:XLFormRowDescriporListTitle title:title[i]];
        //value
        row.value = list[i];
        [section addFormRow:row];
    }

然后實(shí)現(xiàn)通知
直接找到需要改變的cell的tag給他賦值傳過(guò)來(lái)的高度

-(void)setFormCellHeight:(NSNotification *)notification{
    NSDictionary*dic = notification.object;
    XLFormRowDescriptor *row = [self.form formRowWithTag:dic[@"data"]];
    row.height = [dic[@"height"] floatValue];
}
注意

主要就是在自定義cell里面的問(wèn)題了。
繼承XLFrom的BaseCell
里面有個(gè)configure配置cell里的界面

-(void)configure{
    [super configure];
//這個(gè)地方就是需要搭建的界面的代碼
    [self setBackgroundView];
}

然后在BaseCell里面有個(gè)update給定義的視圖做賦值操作

-(void)update{
    [super update];
//先賦值 然后獲取到整體界面的高度
//獲取到之后取到高度 
//然后取到當(dāng)前Cell的tag   self.rowDescriptor.tag
    NSString *height = [NSString stringWithFormat:@"%lf",_cellHeight+40];
    NSDictionary *notificationData = @{@"data":self.rowDescriptor.tag,@"height":height};
//最后通知過(guò)去就可以了
    [[NSNotificationCenter defaultCenter]postNotificationName:setFromCellHeightNSNotification object:notificationData];
}
QQ20170516-090957@2x.png

最后XLFrom一些簡(jiǎn)單的用法我之前的文章里面有寫(xiě)到過(guò),可以去看看

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容