// // CategoryPriceViewController.m // iSales-NPD // // Created by Jack on 2016/10/11. // Copyright © 2016年 United Software Applications, Inc. All rights reserved. // #import "CategoryPriceViewController.h" #import "const.h" #import "CategoryPriceCell.h" #import "UIColor+JK_HEX.h" @interface CategoryPriceViewController () @property (strong, nonatomic) IBOutlet UITableView *categoryTable; @end @implementation CategoryPriceViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self configAppearance]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)configAppearance { self.navigationController.navigationBar.translucent = NO; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( returnBackClick:)]; self.navigationItem.leftBarButtonItem = backButton; } #pragma mark - action - (void)returnBackClick:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.categoryGivenPrice objectForKey:@"count"] integerValue]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *sectionKey = [NSString stringWithFormat:@"category_%ld",section]; NSInteger rows = [[[self.categoryGivenPrice objectForKey:sectionKey] objectForKey:@"count"] integerValue]; if (rows == 0) { rows = 1; } return rows; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CategoryPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CategoryPriceCell"]; // 解析数据 NSInteger section = indexPath.section; NSInteger row = indexPath.row; NSDictionary *categorySection = [self.categoryGivenPrice objectForKey:[NSString stringWithFormat:@"category_%ld",section]]; NSDictionary *categoryRow = [categorySection objectForKey:[NSString stringWithFormat:@"category_%ld",row]]; NSInteger rows = [[[self.categoryGivenPrice objectForKey:[NSString stringWithFormat:@"category_%ld",section]] objectForKey:@"count"] integerValue]; if (rows == 0) { categoryRow = categorySection; } cell.categoryPrice = categoryRow; return cell; } #pragma mark - delegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 47.0f; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 40.0f; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { NSString *sectionKey = [NSString stringWithFormat:@"category_%ld",section]; UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 40)]; headerView.backgroundColor = [UIColor colorWithHEX:0xEFEAE5]; UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 180, 20)]; categoryLabel.textColor = [UIColor blackColor]; categoryLabel.font = [UIFont systemFontOfSize:17.0f]; categoryLabel.textAlignment = NSTextAlignmentLeft; categoryLabel.text = [[self.categoryGivenPrice objectForKey:sectionKey] objectForKey:@"title"]; [headerView addSubview:categoryLabel]; return headerView; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { CategoryPriceCell *cell = [tableView cellForRowAtIndexPath:indexPath]; SetCategoryPriceController *setPriceVC =[ [UIStoryboard storyboardWithName:@"CUL" bundle:nil] instantiateViewControllerWithIdentifier:@"SetCategoryPriceController"]; __weak typeof(self) weakself = self; setPriceVC.returnBlock = ^(NSDictionary *result){ if (weakself) { __strong typeof(weakself) strongself = weakself; NSInteger section = indexPath.section; NSInteger row = indexPath.row; NSDictionary *categorySection = [[self.categoryGivenPrice objectForKey:[NSString stringWithFormat:@"category_%ld",section]] mutableCopy]; NSInteger rows = [[[self.categoryGivenPrice objectForKey:[NSString stringWithFormat:@"category_%ld",section]] objectForKey:@"count"] integerValue]; if (rows == 0) { categorySection = result; } else { [categorySection setValue:result forKey:[NSString stringWithFormat:@"category_%ld",row]]; } [strongself.categoryGivenPrice setObject:categorySection forKey:[NSString stringWithFormat:@"category_%ld",section]]; dispatch_async(dispatch_get_main_queue(), ^{ [strongself.categoryTable reloadData]; }); } }; setPriceVC.categoryID = [[cell.categoryPrice objectForKey:@"cid"] integerValue]; setPriceVC.categoryPrice = cell.categoryPrice.mutableCopy; [self.navigationController pushViewController:setPriceVC animated:YES]; } @end