ZFJFormKit,經過整合數據和事件爲一個Model來配置不一樣類型的Cell來動態設置UITableView。git
ZFJFormKit | 地址 |
---|---|
項目地址 | github.com/zfjsyqk/ZFJ… |
Demo地址: | gitee.com/zfj1128/ZFJ… |
博客地址: | zfj1128.blog.csdn.net/article/det… |
主要經過ZFJFormModel來配置每個Cell,這裏面能夠配置Cell的值和事件;還能夠經過ZFJFormCellConfig來配置Cell的通用屬性,固然也能夠爲每一個不一樣的Cell設置不一樣的ZFJFormCellConfig, ZFJFormConfig類主要用於設置ZFJFormTableView的相關屬性;項目的因此Cell都繼承於ZFJFormCell; ZFJFormKit經過ZFJFormCell、ZFJFormModel和ZFJFormCellConfig來設置ZFJFormCell,而後又經過ZFJFormConfig來配置ZFJFormTableView,經過ZFJFormCell和ZFJFormTableView來實現咱們想要的表單Form。 ZFJPlacehoderTextView是自定義帶佔位符placeholder的textView。 具體結構圖以下: github
ZFJFormKit包含6中CELL類型,具體類型以下:數組
typedef NS_ENUM(NSInteger, ZFJFormCellType) {
KFormCellLabelType = 0, //信息展現
KFormCellHeadImgType = 1, //右邊頭像
KFormCellTextFieldType = 2, //單行輸入
KFormCellTextViewType = 3, //多行輸入
KFormCellSwitchType = 4, //右側開關
KFormCellCustomType = 5 //自定義CELL
};
複製代碼
//CELL的通用配置Model,也能夠根據不一樣的CELL分別配置
ZFJFormCellConfig *configModel = [[ZFJFormCellConfig alloc] init];
//左邊title
configModel.titleColor = [UIColor blackColor];
configModel.titleFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
//頭像圓角尺寸
configModel.headImgRadius = 5;
//CELL右邊值得顏色和字體
configModel.valueColor = [UIColor blueColor];
configModel.valueFont = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
//分割線的配置
configModel.separatorLineColor = [UIColor groupTableViewBackgroundColor];
configModel.isHiddenLine = NO;
//佔位符顏色
configModel.placeholderColor = [UIColor colorWithRed:0.776 green:0.776 blue:0.800 alpha:1.00];
//控件左右兩邊的間距
configModel.marginSize = 15;
複製代碼
//姓名 不能修改,因此不能輸入
ZFJFormModel *name_model = [[ZFJFormModel alloc] init];
name_model.formCellType = KFormCellLabelType;
name_model.configModel = _configModel;
name_model.title = @"姓名";
name_model.value = @"張福傑";
name_model.height = 50;
[self.dataArray addObject:name_model];
複製代碼
/**
頭像(UIImage或者NSString或者NSData)
*/
@property (nonatomic,strong) NSObject *iconObject;
複製代碼
ZFJFormModel *headImg_model = [[ZFJFormModel alloc] init];
//CELL類型
headImg_model.formCellType = KFormCellHeadImgType;
//CELL的通用配置Model,也能夠根據不一樣的CELL分別配置
headImg_model.configModel = configModel;
headImg_model.title = @"頭像";
headImg_model.iconObject = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561278644354&di=3cc92ef55c2336b29b1fe09cbf614705&imgtype=0&src=http%3A%2F%2Fimg4q.duitang.com%2Fuploads%2Fitem%2F201408%2F08%2F20140808171354_XkhfE.jpeg";
headImg_model.height = 100;
headImg_model.isCanSelect = YES;
headImg_model.isShowCellRightImg = YES;
[self.dataArray addObject:headImg_model];
複製代碼
//暱稱(單行輸入 KFormCellTextFieldType)
ZFJFormModel *nickName_model = [[ZFJFormModel alloc] init];
nickName_model.formCellType = KFormCellTextFieldType;
nickName_model.configModel = configModel;
nickName_model.title = @"暱稱";
nickName_model.placeholder = @"請輸入您的暱稱";
nickName_model.height = 50;
nickName_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
if(model.value <= 0){
[MBProgressHUD SHOWPrompttext:model.placeholder];
return NO;
}
return YES;
};
[self.dataArray addObject:nickName_model];
複製代碼
//我的簡介(KFormCellTextViewType 多行輸入)
ZFJFormModel *introduction_model = [[ZFJFormModel alloc] init];
introduction_model.formCellType = KFormCellTextViewType;
introduction_model.configModel = configModel;
introduction_model.title = @"我的簡介";
introduction_model.placeholder = @"請輸入您的我的簡介";
introduction_model.height = 50;
introduction_model.textView_maxHeight = 100;
introduction_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
if(model.value <= 0){
[MBProgressHUD SHOWPrompttext:model.placeholder];
return NO;
}
return YES;
};
[self.dataArray addObject:introduction_model];
複製代碼
//選擇器(KFormCellSwitchType)
ZFJFormModel *switch_model = [[ZFJFormModel alloc] init];
switch_model.formCellType = KFormCellSwitchType;
switch_model.configModel = configModel;
switch_model.title = @"是否開啓好友推薦";
switch_model.placeholder = @"請選擇";
switch_model.height = 50;
switch_model.validateBlock = ^BOOL(ZFJFormModel * _Nullable model) {
if(model.value == nil){
[MBProgressHUD SHOWPrompttext:model.placeholder];
return NO;
}
return YES;
};
[self.dataArray addObject:switch_model];
複製代碼
//自定義CELL(KFormCellCustomType 保存)
ZFJFormModel *custom_model = [[ZFJFormModel alloc] init];
custom_model.formCellType = KFormCellCustomType;
custom_model.configModel = configModel;
custom_model.customCls = [SaveCell class];
custom_model.height = 120;
custom_model.isCanSelect = YES;
//自定義CELL事件處理
custom_model.customCellEventBlock = ^(id _Nonnull obj) {
NSLog(@"obj == %@",obj);
[ZFJFormTool validateDataArray:self.dataArray];
};
[self.dataArray addObject:custom_model];
複製代碼
自定義Cell的設置以下:bash
- (void)configCellWithModel:(ZFJFormModel *)model{
NSLog(@"aaaaaaa");
_model = model;
}
- (void)saveBtnClick:(UIButton *)button{
//自定義CELL的事件處理
if(_model != nil && _model.customCellEventBlock){
_model.customCellEventBlock(button);
}
}
複製代碼
- (ZFJFormTableView *)tableView{
if (_tableView == nil){
ZFJFormConfig *formConfig = [[ZFJFormConfig alloc] init];
formConfig.backgroundColor = [UIColor groupTableViewBackgroundColor];
_tableView = [[ZFJFormTableView alloc] initWithFrame:CGRectMake(0, KNavBarHei, ZFJForm_ScreenWidth, ZFJForm_ScreenHeight - KNavBarHei) config:formConfig];
}
return _tableView;
}
複製代碼
事件處理接收架構
_tableView.didSelectRowBlock = ^(NSIndexPath * _Nullable indexPath, ZFJFormModel * _Nullable model) {
NSLog(@"%@",model);
};
複製代碼
[ZFJFormTool validateDataArray:self.dataArray];
複製代碼
- (ZFJPlacehoderTextView *)textView{
if(_textView == nil){
_textView = [[ZFJPlacehoderTextView alloc] init];
_textView.font = [UIFont systemFontOfSize:14];
_textView.delegate = self;
_textView.textAlignment = NSTextAlignmentRight;
_textView.placeholder = @"這是提示文字";
}
return _textView;
}
複製代碼
閒來無事,把我在上個項目中本身封裝表單配置框架抽出來,封裝拿給你們使用,也歡迎各位大神提出寶貴的意見和建議,也歡迎你們進羣交流365152048!框架