ReceiveProductViewController.m 8.74 KB
Newer Older
陈俊俊's avatar
陈俊俊 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//
//  ReceiveProductViewController.m
//  XFFruit
//
//  Created by 陈俊俊 on 15/10/15.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ReceiveProductViewController.h"
#import "HeaderCell.h"
#import "ReceiveProductCell.h"
#import "PurchaseBillProduct.h"
#define TableHeight 44
#define ShowHeight  130
Sandy's avatar
Sandy committed
15
#define HeaderHeight 40
陈俊俊's avatar
陈俊俊 committed
16 17 18 19 20 21

@interface ReceiveProductViewController ()<UITableViewDataSource,UITableViewDelegate,HeaderCellDelegate>
{
    CGRect _tableFrame;
    NSMutableArray *_selectRowArr;//记录当前选中的cell
}
Sandy's avatar
Sandy committed
22 23 24 25
//采购单号
@property (strong, nonatomic) NSMutableArray *arrBillNumber;
//按照采购单号区分之后的数据
@property (strong, nonatomic) NSMutableDictionary *dictData;
陈俊俊's avatar
陈俊俊 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
@end

@implementation ReceiveProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor  = XXFBgColor;
    [super viewDidLoad];
    [self initData];
    [self createView];
}
- (void)initData{
    _selectRowArr = [[NSMutableArray alloc]init];
    if (!self.productArr) {
        self.productArr = [NSMutableArray array];
    }
}

Sandy's avatar
Sandy committed
44 45 46 47 48 49 50 51 52 53 54
- (void)setProductArr:(NSMutableArray *)productArr {
    _productArr = productArr;
    [self convertData];
}

- (void)convertData {
    self.arrBillNumber = [NSMutableArray array];
    self.dictData = [NSMutableDictionary dictionary];
    for (TransferPdtDetail *pdt in self.productArr) {
        NSString *sourceBillNumber = pdt.sourcebillnumber ? pdt.sourcebillnumber : @" ";
        
Sandy's avatar
Sandy committed
55
        if (![self.arrBillNumber containsObject:sourceBillNumber]) {
Sandy's avatar
Sandy committed
56 57 58 59 60 61 62 63 64 65 66
            [self.arrBillNumber addObject:sourceBillNumber];
        }
        
        NSMutableArray *arrTemp = [self.dictData objectForKey:sourceBillNumber];
        if (!arrTemp) {
            arrTemp = [NSMutableArray array];
            [self.dictData setObject:arrTemp forKey:sourceBillNumber];
        }
        
        [arrTemp addObject:pdt];
    }
Sandy's avatar
Sandy committed
67
    CLog(@"%@", self.dictData);
Sandy's avatar
Sandy committed
68 69 70
}


陈俊俊's avatar
陈俊俊 committed
71 72
- (void)setViewFrame:(CGRect)viewFrame{
    _tableFrame = viewFrame;
Sandy's avatar
Sandy committed
73
    _tableView.frame = viewFrame;
陈俊俊's avatar
陈俊俊 committed
74 75 76 77 78 79 80 81
}
- (void)createView{
    self.tableView = [[UITableView alloc]initWithFrame:_tableFrame style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
82
    NSArray *arr = @[@"商品",@"规格", @"待收包装数",@"已收包装数"];
陈俊俊's avatar
陈俊俊 committed
83 84 85 86 87 88 89 90
    
    HeaderCell *headCell = [[HeaderCell alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, 38) withArr:arr withHiddenEdit:self.isHiddenAdd];
    [self.view addSubview:headCell];
    headCell.delegate = self;
    self.tableView.tableHeaderView = headCell;
}

- (void)editClick:(UIButton *)btn{
Sandy's avatar
Sandy committed
91 92 93 94 95 96
    NSInteger section = btn.tag / 1000;
    NSInteger row = btn.tag % 1000;
    NSMutableArray *arrSection = [self.dictData objectForKey:self.arrBillNumber[section]];
    
    TransferPdtDetail *pdDetail = arrSection[row];
    NSInteger index = [self.productArr indexOfObject:pdDetail];
97
    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_EditReceiveProduct object:nil userInfo:@{@"transferPdtDetail":pdDetail,@"indexPath":@(index)}];
陈俊俊's avatar
陈俊俊 committed
98 99 100
}
#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
Sandy's avatar
Sandy committed
101
    return self.arrBillNumber.count;
陈俊俊's avatar
陈俊俊 committed
102
}
Sandy's avatar
Sandy committed
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

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSString *sourceBillNumber = self.arrBillNumber[section];
    if (sourceBillNumber.length == 1) {
        return 0;
    }else{
        return HeaderHeight;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, HeaderHeight)];
    header.backgroundColor = [UIColor whiteColor];
    UILabel *transportBillNumber = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 250, HeaderHeight)];
    [header addSubview:transportBillNumber];
    transportBillNumber.text = [NSString stringWithFormat:@"[采购单:%@]",
                                self.arrBillNumber[section]];
    transportBillNumber.textColor = GXF_GREEN_COLOR;
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, HeaderHeight - 1, IBT_MAIN_SCREEN_WIDTH, 1)];
    line.backgroundColor = [UIColor colorWithWhite:0.835 alpha:1.000];
    [header addSubview:line];
    //删除按钮
//    UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeCustom];
//    btnDelete.frame = CGRectMake(IBT_MAIN_SCREEN_WIDTH - 16 - 30, (HeaderHeight - 30)/2, 30, 30);
//    [btnDelete setImage:[UIImage imageNamed:@"xf_delete_pic"] forState:UIControlStateNormal];
//    [header addSubview:btnDelete];
//    btnDelete.tag = section;
//    [btnDelete addTarget:self action:@selector(actionDeleteBillNumber:) forControlEvents:UIControlEventTouchUpInside];
    
    return header;
}

陈俊俊's avatar
陈俊俊 committed
136
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Sandy's avatar
Sandy committed
137 138
    NSMutableArray *arrSection = [self.dictData objectForKey:self.arrBillNumber[section]];
    return arrSection.count;
陈俊俊's avatar
陈俊俊 committed
139
}
Sandy's avatar
Sandy committed
140

陈俊俊's avatar
陈俊俊 committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"ReceiveProductCell";
    ReceiveProductCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[ReceiveProductCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.isHiddenEdit) {
            cell.editBtn.hidden = YES;
        }
    }
    if ([self isHaveIndexPath:indexPath]) {
        cell.smallImageView.image = [UIImage imageNamed:@"arrowdown"];
        CGRect Linefrmame = cell.lineLabel.frame;
        Linefrmame.origin.y = ShowHeight + TableHeight -1;
        cell.lineLabel.frame = Linefrmame;
        CGRect showfrmame = cell.showView.frame;
        showfrmame.size.height = ShowHeight;
        cell.showView.frame = showfrmame;
        cell.backgroundColor = XXFBgColor;
    }else{
        cell.smallImageView.image = [UIImage imageNamed:@"arrowright"];
        CGRect Linefrmame = cell.lineLabel.frame;
        Linefrmame.origin.y = TableHeight-1;
        cell.lineLabel.frame = Linefrmame;
        CGRect showfrmame = cell.showView.frame;
        showfrmame.size.height = 0;
        cell.showView.frame = showfrmame;
        cell.backgroundColor = [UIColor whiteColor];
    }
Sandy's avatar
Sandy committed
170
    cell.editBtn.tag = indexPath.section * 1000 + indexPath.row;
陈俊俊's avatar
陈俊俊 committed
171 172 173
    [cell.editBtn addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
    
    if (self.productArr.count > 0) {
Sandy's avatar
Sandy committed
174 175
        NSMutableArray *arrSection = [self.dictData objectForKey:self.arrBillNumber[indexPath.section]];
        TransferPdtDetail *billP = arrSection[indexPath.row];
陈俊俊's avatar
陈俊俊 committed
176 177
        [cell setBillProduct:billP row:indexPath.row defaultState:self.defaultState[indexPath.row]];
    }
Sandy's avatar
Sandy committed
178
//    cell.backgroundColor = [UIColor redColor];
陈俊俊's avatar
陈俊俊 committed
179 180 181 182
    return cell;
}
- (BOOL)isHaveIndexPath:(NSIndexPath *)indexPath{
    for (NSIndexPath *path in _selectRowArr) {
Sandy's avatar
Sandy committed
183
        if (path == indexPath) {
陈俊俊's avatar
陈俊俊 committed
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
            return YES;
        }
    }
    return NO;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReceiveProductCell *cell = (ReceiveProductCell *)[tableView cellForRowAtIndexPath:indexPath];
    CGRect Linefrmame = cell.lineLabel.frame;
    CGRect showfrmame = cell.showView.frame;
    if (Linefrmame.origin.y == TableHeight - 1) {
        cell.smallImageView.image = [UIImage imageNamed:@"arrowdown"];
        
        Linefrmame.origin.y = ShowHeight + TableHeight - 1;
        showfrmame.size.height = ShowHeight;
        cell.backgroundColor = [UIColor whiteColor];
        [_selectRowArr addObject:indexPath];
    }else{
        cell.smallImageView.image = [UIImage imageNamed:@"arrowright"];
        Linefrmame.origin.y = TableHeight -1;
        showfrmame.size.height = 0;
        cell.backgroundColor = [UIColor whiteColor];
        [_selectRowArr removeObject:indexPath];
    }
    cell.lineLabel.frame = Linefrmame;
    cell.showView.frame = showfrmame;
    [self.tableView reloadData];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self isHaveIndexPath:indexPath]) {
        return ShowHeight + TableHeight;
    }
    return TableHeight;
}


- (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