AddProductViewController.m 11 KB
Newer Older
n22's avatar
n22 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
//
//  AddProductViewController.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/8/27.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "AddProductViewController.h"
#import "SurveyCell.h"
#import "ChooseProductViewController.h"
#import "ChooseTypeViewController.h"

#define LeftMargin 15
#define BtnHeight 44
#define TableHeight 46
#define TopHeight 50
#define TotalHeight 508
#define KeyboardHeight 258


typedef enum : NSUInteger {
    DelTag = 9000,
    CommitTag,
    DissTag
} BtnTag;

@interface AddProductViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,HPGrowingTextViewDelegate>
{
    UIView *_bgView;
    UITableView *_tableView;
    NSMutableArray *_dataArr;
    CGRect _tableFrame;
}

@end

@implementation AddProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self bulidLayout];
}
- (void)initData{
    _dataArr = [NSMutableArray array];
    [_dataArr addObject:@"商品"];
    [_dataArr addObject:@"包装单位"];
    [_dataArr addObject:@"包装规格"];
    [_dataArr addObject:@"包装数量"];
    [_dataArr addObject:@"基础数量"];
    [_dataArr addObject:@"基础单价"];
    [_dataArr addObject:@"包装单价"];
    [_dataArr addObject:@"总金额"];
    [_dataArr addObject:@"备注"];
}
- (void)setViewFrame:(CGRect)viewFrame{
    _tableFrame = viewFrame;
}
- (void)bulidLayout{
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(LeftMargin, (self.view.frame.size.height - TotalHeight)/2, self.view.frame.size.width - LeftMargin*2, TotalHeight)];
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    [self.view addSubview:_bgView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,CGRectGetWidth(_bgView.frame), TopHeight)];
    label.text = @"新增商品明细";
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = XXFBgColor;
陈俊俊's avatar
陈俊俊 committed
70 71
    label.textColor = GXF_CONTENT_COLOR;
    label.font = GXF_FIFTEENTEN_SIZE;
n22's avatar
n22 committed
72 73
    [_bgView addSubview:label];
    
陈俊俊's avatar
陈俊俊 committed
74 75
    UIButton *dissBtn =  [IBTCustomButtom creatButtonWithFrame:CGRectMake(CGRectGetWidth(_bgView.frame) - TopHeight, 0, TopHeight, TopHeight) target:self sel:@selector(btnClick:) tag:DissTag image:nil title:@"关闭" titleColor:GXF_CONTENT_COLOR isCorner:NO corner:0 bgColor:nil];
    dissBtn.titleLabel.font = GXF_FIFTEENTEN_SIZE;
n22's avatar
n22 committed
76 77 78 79 80 81 82 83 84 85 86
    [_bgView addSubview:dissBtn];
    
    
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, TopHeight,CGRectGetWidth(_bgView.frame), CGRectGetHeight(_bgView.frame)-TopHeight)) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.bounces = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, 50)];
    
陈俊俊's avatar
陈俊俊 committed
87
    UIButton *delBtn =  [IBTCustomButtom creatButtonWithFrame:CGRectMake(LeftMargin, 10, (CGRectGetWidth(_bgView.frame) - LeftMargin*3)/2, BtnHeight) target:self sel:@selector(btnClick:) tag:6001 image:nil title:@"删除" titleColor: [UIColor whiteColor] isCorner:YES corner:5 bgColor:GXF_SAVE_COLOR];
n22's avatar
n22 committed
88 89
    [footView addSubview:delBtn];
    
陈俊俊's avatar
陈俊俊 committed
90
    UIButton *commitBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(delBtn.frame.origin.x + delBtn.frame.size.width + LeftMargin, 10, (CGRectGetWidth(_bgView.frame) - LeftMargin*3)/2, BtnHeight) target:self sel:@selector(btnClick:) tag:1001 image:nil title:@"提交" titleColor: [UIColor whiteColor] isCorner:YES corner:5 bgColor:GXF_COMMIT_COLOR];
n22's avatar
n22 committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    [footView addSubview:commitBtn];
    
    //    [self.view addSubview:footView];
    _tableView.tableFooterView = footView;
    [_bgView addSubview:_tableView];
}
- (void)btnClick:(UIButton *)btn{
    switch (btn.tag) {
        case DissTag:
        {
            if ([self.delegate respondsToSelector:@selector(dissmiss)]) {
                [self.delegate dissmiss];
            }
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    SurveyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[SurveyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row ==0 || indexPath.row == 1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        [self createViewInCell:cell indexPath:indexPath];
    }
    [cell setTitleStr:_dataArr[indexPath.row]];
    return cell;
}
-  (void)createViewInCell:(SurveyCell *)cell indexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0 || indexPath.row == 1 ) {
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:(CGRectMake(100+LeftMargin, 0, CGRectGetWidth(_bgView.frame) - 100 - LeftMargin*2-10, TableHeight))];
        contentLabel.textAlignment= NSTextAlignmentRight;
陈俊俊's avatar
陈俊俊 committed
139 140
        contentLabel.textColor = GXF_PLACEHOLDER_COLOR;
        contentLabel.font = GXF_FIFTEENTEN_SIZE;
n22's avatar
n22 committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        [cell.contentView addSubview:contentLabel];
        if (indexPath.row == 0) {
            contentLabel.text = @"选择商品";
            self.productNameLabel = contentLabel;
            
        }else if(indexPath.row == 1){
            contentLabel.text = @"选择包装单位";
            self.productTypeLabel = contentLabel;
            
        }
    }else if (indexPath.row == _dataArr.count -1){
        self.remarkTextView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(100+LeftMargin, 0,  CGRectGetWidth(_bgView.frame) - 100 - LeftMargin*2-10, TableHeight)];
        self.remarkTextView.contentInset = UIEdgeInsetsMake(5, 5, 5, 0);
        self.remarkTextView.minNumberOfLines = 1;
        self.remarkTextView.maxNumberOfLines = 2;
陈俊俊's avatar
陈俊俊 committed
156
        self.remarkTextView.font = GXF_FIFTEENTEN_SIZE;
n22's avatar
n22 committed
157 158 159 160 161 162 163 164 165
        self.remarkTextView.textAlignment = NSTextAlignmentRight;
        self.remarkTextView.delegate = self;
        self.remarkTextView.returnKeyType = UIReturnKeyDone;
        self.remarkTextView.placeholder = @"输入备注内容";
        [cell.contentView addSubview:self.remarkTextView];
    }else{
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100+LeftMargin, 0,  CGRectGetWidth(_bgView.frame) - 100 - LeftMargin*2-30, TableHeight)];
        textField.textAlignment = NSTextAlignmentRight;
陈俊俊's avatar
陈俊俊 committed
166 167
        textField.textColor = GXF_CONTENT_COLOR;
        textField.font = GXF_FIFTEENTEN_SIZE;
n22's avatar
n22 committed
168 169 170 171 172
        textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        textField.returnKeyType = UIReturnKeyDone;
        textField.delegate = self;
        [cell.contentView addSubview:textField];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(textField.frame) + 5, 0, 20, TableHeight)];
陈俊俊's avatar
陈俊俊 committed
173 174
        label.textColor = GXF_CONTENT_COLOR;
        label.font = GXF_FIFTEENTEN_SIZE;
n22's avatar
n22 committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
        [cell.contentView addSubview:label];
        if (indexPath.row == 2) {
            label.text = @"斤";
            textField.placeholder = @"包装规格";
            self.productStandFiled = textField;
        }else if (indexPath.row == 3) {
            label.text = @"框";
            textField.placeholder = @"包装数量";
            self.productCountFiled = textField;

        }else if (indexPath.row == 4) {
            label.text = @"斤";
            textField.placeholder = @"基础数量";
            self.baseCountFiled = textField;
            
        }else if (indexPath.row == 5) {
            label.text = @"元";
            textField.placeholder = @"基础单价";

            self.basePriceFiled = textField;
        }else if (indexPath.row == 6) {
            label.text = @"元";
            textField.placeholder = @"包装单价";
            self.productPriceFiled = textField;
        }else if (indexPath.row == 7) {
            label.text = @"元";
            textField.placeholder = @"总金额";
            self.totalPriceFiled = textField;
        }
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        ChooseProductViewController *cvc = [ChooseProductViewController new];
        cvc.choseProduct = ^(Product *product){
            self.productNameLabel.text = [NSString stringWithFormat:@"%@[%@]",product.name,product.code];
陈俊俊's avatar
陈俊俊 committed
211
            self.productNameLabel.textColor = GXF_CONTENT_COLOR;
n22's avatar
n22 committed
212 213 214 215 216 217 218 219 220
        };
        cvc.isMoreChose = NO;
        [self.delegate pushViewController:cvc];
    }else if (indexPath.row == 1){
        ChooseTypeViewController *tvc = [[ChooseTypeViewController alloc]init];
        tvc.title = @"类型";
        tvc.dataArr = @[@"筐",@"斤"];
        tvc.choseType = ^(NSString *type){
            self.productTypeLabel.text = type;
陈俊俊's avatar
陈俊俊 committed
221
            self.productTypeLabel.textColor = GXF_CONTENT_COLOR;
n22's avatar
n22 committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        };
        [self.delegate pushViewController:tvc];
    }
}




#pragma mark delegate
- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
    [self.remarkTextView resignFirstResponder];
    [self setTableFrame:TopHeight];
    return YES;
}
- (void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView{
    CGFloat offset = ScreenSize.height - TotalHeight - KeyboardHeight;
    if (offset < 0) {//上移
        [self setTableFrame:TopHeight+offset];
    }
}
- (void)setTableFrame:(CGFloat)tabelH{
    [UIView animateWithDuration:0.25 animations:^{
        CGRect tableFrame = _tableView.frame;
        tableFrame.origin.y = tabelH;
        _tableView.frame = tableFrame;
    }];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.basePriceFiled || textField == self.productPriceFiled || textField == self.totalPriceFiled) {
        CGFloat offset = ScreenSize.height - TotalHeight - KeyboardHeight;
        if (offset < 0) {//上移
            [self setTableFrame:TopHeight+offset];
        }
    }
}

- (void)keyboardHidden{
    [self.productStandFiled resignFirstResponder];
    [self.productPriceFiled resignFirstResponder];
    [self.productCountFiled resignFirstResponder];
    [self.basePriceFiled resignFirstResponder];
    [self.baseCountFiled resignFirstResponder];
    [self.totalPriceFiled resignFirstResponder];
    [self setTableFrame:TopHeight];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end