iOS 优化VC之自定义cell(一)

年后回来上了个新版本,主要是添加自动投标功能。因为工期紧,只是把功能实现了,没有考虑太多的结构优化。大部分的subview和逻辑都放在了vc里面,这样的恶果是出了问题,特别难定位,一团乱。故乘刚发完新版 新功能又还没确定间隙,初步把该功能vc优化了下。


iOS 优化VC之自定义cell(一)_第1张图片
效果图

大致思路

自定义cell(cell里面根据row不同,设置cell上不同的subview);
vc将默认信息model传给自定义cell;
自定义cell将用户输入的信息回调给vc;
最后vc处理相应逻辑,请求后台接口。

涉及到的控件:textfield,pickview(集成开源三方),button互斥,uiswich等

ViewController(简称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;
        //开启全投后 对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 
#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 实现文件(因篇幅有限,只摘取了大概框架代码)

+ (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{
}

//全投开关
- (void)setupAllCell{
}

//标的种类
- (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- 标的种类回调
- (void)btnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (self.blockBidType) {
        self.blockBidType(btn.tag,btn.selected);
    }
    //实现状态互斥功能
    for (UIButton *cBtn in arrBtn) {
        if (cBtn.tag != btn.tag) {
            cBtn.selected = NO;
        }
    }
}

#pragma mark- 限定金额回调
- (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- 全投开关回调
- (void)swChange:(UISwitch *)sw{
    
    if (self.blockAllAmount) {
        self.blockAllAmount(sw.on);
    }
}

#pragma mark- 选择器(利率和期限)回调
- (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个月", @"2个月", @"3个月", @"4个月", @"5个月", @"6个月", @"7个月", @"8个月", @"9个月", @"10个月", @"11个月", @"12个月"],
                        @[@"不限",@"1个月", @"2个月", @"3个月", @"4个月", @"5个月", @"6个月", @"7个月", @"8个月", @"9个月", @"10个月", @"11个月", @"12个月"]
                        ];
    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);
        }
    }];
}

感想

简单封装优化之后,后期若需扩展,就比较容易:
若更新UI 直接修改cell文件;
若修改相应逻辑 直接修改vc即可等
这样结构相对也比较清晰。

值得记录的问题:
1 button互斥
思路:遍历所有的button,若不是当前所选button,让其selected为NO
2 全投状态 更新限定金额输入框text及enable状态
思路:在开关的回调中拿到 输入框cell,之后再对该cell上的tf做相应更新([tb reload]是不行的)

不足处:
回调block太多;
cell不够明晰等
待后续更好的优化

后续:
其实后来想想这种static 不重用的cell ,封装一个带xib的tableview即可,没必要像上面那样自定义那么多cell

你可能感兴趣的:(iOS 优化VC之自定义cell(一))