之前一直在寫(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ò),可以去看看