年后回來(lái)上了個(gè)新版本,主要是添加自動(dòng)投標(biāo)功能。因?yàn)楣て诰o,只是把功能實(shí)現(xiàn)了,沒(méi)有考慮太多的結(jié)構(gòu)優(yōu)化。大部分的subview和邏輯都放在了vc里面,這樣的惡果是出了問(wèn)題,特別難定位,一團(tuán)亂。故乘剛發(fā)完新版 新功能又還沒(méi)確定間隙,初步把該功能vc優(yōu)化了下。

大致思路
自定義cell(cell里面根據(jù)row不同,設(shè)置cell上不同的subview);
vc將默認(rèn)信息model傳給自定義cell;
自定義cell將用戶輸入的信息回調(diào)給vc;
最后vc處理相應(yīng)邏輯,請(qǐng)求后臺(tái)接口。
涉及到的控件:textfield,pickview(集成開(kāi)源三方),button互斥,uiswich等
ViewController(簡(jiǎn)稱(chēng)vc)核心代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AutoInvesCustomCell *cell = [AutoInvesCustomCell cellWithTableView:tableView indexPath:indexPath];
cell.investInfo = investInfo;
cell.blockPick = ^(NSString *strResult, NSInteger row, AutoInvesCustomCell *cell) {
if (row == 1) {
strRate = strResult;
cell.lbRate.text = strResult;
}else{
strMonth = strResult;
cell.lbMonth.text = strResult;
}
};
cell.blockBidType = ^(NSInteger tag, BOOL isSelected) {
isBidQrb = ((tag == 101 && isSelected) ? YES:NO);
isBidXiny = ((tag == 102 && isSelected) ? YES:NO);
if (tag == 100 && isSelected) {
isBidQrb = YES;
isBidXiny = YES;
}
};
cell.blockAmount = ^(NSString *text) {
strAmount = text;
};
cell.blockAllAmount = ^(BOOL isOn) {
isRichMan = isOn;
//開(kāi)啟全投后 對(duì)tf的處理
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
AutoInvesCustomCell *tfCell = [tableView cellForRowAtIndexPath:indexPath];
tfCell.tfAmount.enabled = !isRichMan;
tfCell.tfAmount.text = isRichMan? @"全投":strAmount;
};
return cell;
}
自定義cell頭文件(AutoInvesCustomCell.h)
#import <UIKit/UIKit.h>
#import "AutoInvestModel.h"
#define AddBottomLine(view) CGFloat lineHeight = 0.5f;\
CGRect lineFrame = CGRectMake(ContentX, HeightNormalCell-lineHeight, kScreenWidth-2*ContentX, lineHeight);\
UIView *lineNavBottom = [[UIView alloc] initWithFrame:lineFrame];\
lineNavBottom.backgroundColor = kColorLineSeperator;\
[view addSubview:lineNavBottom];
//宏定義
@class AutoInvesCustomCell;
typedef void(^BidTypeCellBlock)(NSInteger tag,BOOL isSelected);
typedef void(^AllAmountSWBlock)(BOOL isOn);
typedef void(^AmountTFBlock)(NSString *text);
typedef void(^PickViewBlock)(NSString *strResult,NSInteger row,AutoInvesCustomCell *cell);
@interface AutoInvesCustomCell : UITableViewCell
@property(strong,nonatomic)AutoInvestInfo *investInfo;
//UI控件屬性
@property(nonatomic,copy)BidTypeCellBlock blockBidType;
@property(nonatomic,copy)AllAmountSWBlock blockAllAmount;
@property(nonatomic,copy)AmountTFBlock blockAmount;
@property(nonatomic,copy)PickViewBlock blockPick;
+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath*)indexPath;
@end
自定義cell 實(shí)現(xiàn)文件(因篇幅有限,只摘取了大概框架代碼)
+ (instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath*)indexPath{
static NSString *ID = @"cellID";
AutoInvesCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(!cell)
{
cell = [[AutoInvesCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.currIndexPath = indexPath;
if (indexPath.section == 0) {
[cell setupBannerCell];
}
if (indexPath.section == 2) {
[cell setupAllCell];
}
if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
[cell setupAmount];
break;
case 1:
[cell setupRate];
break;
case 2:
[cell setupMonth];
break;
case 3:
[cell setupBidMode];
break;
default:
break;
}
}
}
return cell;
}
//banner
- (void)setupBannerCell{
}
//借款期限
- (void)setupMonth{
}
//期待年化收益率
- (void)setupRate{
}
//投資限定金額
- (void)setupAmount{
}
//全投開(kāi)關(guān)
- (void)setupAllCell{
}
//標(biāo)的種類(lèi)
- (void)setupBidMode{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
if (selected) {
if (self.currIndexPath.row == 1
|| self.currIndexPath.row == 2)
[self pickView:self.currIndexPath.row];
}
}
#pragma mark- 用model填充cell
- (void)setInvestInfo:(AutoInvestInfo *)investInfo{
if (!investInfo) {
return;
}
//configcell
}
#pragma mark- 標(biāo)的種類(lèi)回調(diào)
- (void)btnClick:(UIButton *)btn{
btn.selected = !btn.selected;
if (self.blockBidType) {
self.blockBidType(btn.tag,btn.selected);
}
//實(shí)現(xiàn)狀態(tài)互斥功能
for (UIButton *cBtn in arrBtn) {
if (cBtn.tag != btn.tag) {
cBtn.selected = NO;
}
}
}
#pragma mark- 限定金額回調(diào)
- (void)textFieldDone:(UIBarButtonItem *)tf{
UIView *v = [self superview];
if (![self.tfAmount.text isEqualToString:@"全投"]) {
NSRange range = [self.tfAmount.text rangeOfString:@"0"];
if (range.location == 0) {
[MBProgressHUD showError:@"限定金額須大于50" toView:v];
return;
}
if ([self.tfAmount.text doubleValue] <50) {
[MBProgressHUD showError:@"限定金額須大于50" toView:v];
return;
}
}
if (self.blockAmount) {
self.blockAmount(self.tfAmount.text);
}
[self endEditing:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
textField.text = @"";
}
#pragma mark- 全投開(kāi)關(guān)回調(diào)
- (void)swChange:(UISwitch *)sw{
if (self.blockAllAmount) {
self.blockAllAmount(sw.on);
}
}
#pragma mark- 選擇器(利率和期限)回調(diào)
- (void)pickView:(NSInteger)row
{
NSArray *array1 = @[
@[@"不限",@"6%", @"7%", @"8%", @"9%", @"10%", @"11%", @"12%", @"13%", @"14%", @"15%", @"16%", @"17%", @"18%"],
@[@"不限",@"6%", @"7%", @"8%", @"9%", @"10%", @"11%", @"12%", @"13%", @"14%", @"15%", @"16%", @"17%", @"18%"]
];
NSArray *array2 = @[
@[@"不限",@"1個(gè)月", @"2個(gè)月", @"3個(gè)月", @"4個(gè)月", @"5個(gè)月", @"6個(gè)月", @"7個(gè)月", @"8個(gè)月", @"9個(gè)月", @"10個(gè)月", @"11個(gè)月", @"12個(gè)月"],
@[@"不限",@"1個(gè)月", @"2個(gè)月", @"3個(gè)月", @"4個(gè)月", @"5個(gè)月", @"6個(gè)月", @"7個(gè)月", @"8個(gè)月", @"9個(gè)月", @"10個(gè)月", @"11個(gè)月", @"12個(gè)月"]
];
BAKit_WeakSelf
[BAKit_PickerView ba_creatCustomMultiplePickerViewWithDataArray:(row==1 ? array1:array2) configuration:^(BAKit_PickerView *tempView) {
tempView.ba_pickViewTitleColor = BAKit_Color_Black_pod;
// 自定義 pickview title 的字體
tempView.ba_pickViewTitleFont = [UIFont boldSystemFontOfSize:15];
// 可以自由定制 toolBar 和 pickView 的背景顏色
// tempView.ba_backgroundColor_toolBar = kColorWebBg;
tempView.ba_backgroundColor_pickView = [UIColor whiteColor];
tempView.animationType = BAKit_PickerViewAnimationTypeBottom;
tempView.pickerViewPositionType = BAKit_PickerViewPositionTypeNormal;
pickView = tempView;
} block:^(NSString *resultString) {
BAKit_StrongSelf
// BAKit_ShowAlertWithMsg_ios8(resultString);
if (self.blockPick) {
self.blockPick(resultString, row,self);
}
}];
}
感想
簡(jiǎn)單封裝優(yōu)化之后,后期若需擴(kuò)展,就比較容易:
若更新UI 直接修改cell文件;
若修改相應(yīng)邏輯 直接修改vc即可等
這樣結(jié)構(gòu)相對(duì)也比較清晰。
值得記錄的問(wèn)題:
1 button互斥
思路:遍歷所有的button,若不是當(dāng)前所選button,讓其selected為NO
2 全投狀態(tài) 更新限定金額輸入框text及enable狀態(tài)
思路:在開(kāi)關(guān)的回調(diào)中拿到 輸入框cell,之后再對(duì)該cell上的tf做相應(yīng)更新([tb reload]是不行的)
不足處:
回調(diào)block太多;
cell不夠明晰等
待后續(xù)更好的優(yōu)化
后續(xù):
其實(shí)后來(lái)想想這種static 不重用的cell ,封裝一個(gè)帶xib的tableview即可,沒(méi)必要像上面那樣自定義那么多cell