| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- //
- // AttributeTableDataSource.m
- // Granite Expo eSign
- //
- // Created by Jack on 2017/10/11.
- // Copyright © 2017年 United Software Applications, Inc. All rights reserved.
- //
- #import "AttributeTableDataSourceAndDelegate.h"
- #import "AttributeTextFieldCell.h"
- #import "AttributeLabelCell.h"
- #import "AttributeSwitchCell.h"
- #import "DocumentTemplateViewController.h"
- typedef enum {
- CellTypeTextField = 0,
- CellTypeLabel = 1,
- CellTypeSwitch = 2
- }CellType;
- @interface AttributeTableDataSourceAndDelegate ()
- @property (nonatomic,weak) DocumentTemplateViewController *table_vc;
- @property (nonatomic,weak) UITableView *tableView;
- @property (nonatomic,strong) NSMutableDictionary *template_data;
- @property (nonatomic,strong) NSMutableDictionary *data;
- @property (nonatomic,assign) NSInteger data_index;
- @property (nonatomic,assign) NSInteger data_row;
- @property (nonatomic,assign) BOOL check_option;
- @end
- @implementation AttributeTableDataSourceAndDelegate
- - (instancetype)initWithTableView:(UITableView *)tableView Controller:(DocumentTemplateViewController *)viewController {
- if (self = [super init]) {
- _tableView = tableView;
- _table_vc = viewController;
- _tableView.dataSource = self;
- _tableView.delegate = self;
- }
- return self;
- }
- - (void)changeListItemData:(NSMutableDictionary *)data forSection:(NSInteger)section Row:(NSInteger)row {
- self.check_option = NO;
- [self.tableView endEditing:YES];
- self.data_index = section;
- self.data_row = row;
-
- NSString *path = [[NSBundle mainBundle] pathForResource:@"attribute_template.json" ofType:nil];
- NSData *json_data = [NSData dataWithContentsOfFile:path];
- NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
-
- self.template_data = [[template objectForKey:@"List_Item"] mutableCopy];
- [self.template_data removeObjectForKey:@"size&position"];
-
- self.data = data;
-
- [self.tableView reloadData];
- }
- - (void)changeCheckOptionData:(NSMutableDictionary *)data forSection:(NSInteger)section Row:(NSInteger)row {
- self.check_option = YES;
- [self.tableView endEditing:YES];
- self.data_index = section;
- self.data_row = row;
-
- NSString *path = [[NSBundle mainBundle] pathForResource:@"attribute_template.json" ofType:nil];
- NSData *json_data = [NSData dataWithContentsOfFile:path];
- NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
-
- self.template_data = [[template objectForKey:@"Check_Option"] mutableCopy];
- [self.template_data removeObjectForKey:@"size&position"];
-
- self.data = data;
-
- [self.tableView reloadData];
- }
- - (void)changeData:(NSMutableDictionary *)data type:(ItemType)type forIndex:(NSInteger)index {
- self.check_option = NO;
- [self.tableView endEditing:YES]; // 正在编辑状态中,选择其他Item导致编辑信息写入新的Item
- if (data == nil || type == ItemTypeNone) {
- self.data = nil;
- self.template_data = nil;
- [self.tableView reloadData];
- return;
- }
- self.data_index = index;
-
- NSString *path = [[NSBundle mainBundle] pathForResource:@"attribute_template.json" ofType:nil];
- NSData *json_data = [NSData dataWithContentsOfFile:path];
- NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
-
- NSString *key = nil;
- switch (type) {
- case ItemTypeTextView: {
- key = @"TextView";
- }
- break;
- case ItemTypeSignature: {
- key = @"Signature";
- }
- break;
- case ItemTypeImage: {
- key = @"Image";
- }
- break;
- case ItemTypeIgnore: {
- key = @"Ignore";
- }
- break;
- case ItemTypeButton: {
- key = @"Button";
- }
- break;
- case ItemTypeCheck: {
- __block BOOL show_option = NO;
- // NSMutableArray* cadedate = data[@"cadedate"];
- // [cadedate enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- // NSArray* item = (NSArray *)obj;
- // NSArray* checkmarker = item[1];
- // if (checkmarker.count != 0) {
- // show_option = YES;
- // } else {
- // show_option = NO;
- // *stop = YES;
- // }
- // }];
-
- if ([data objectForKey:@"marker_size"] != nil) {
- show_option = YES;
- } else {
- show_option = NO;
- }
-
- if (show_option) {
- key = @"Check_0";
- } else {
- key = @"Check_1";
- }
- }
- break;
- case ItemTypeLabel: {
- key = @"Label";
- }
- break;
- case ItemTypeDatePicker: {
- key = @"DatePicker";
- }
- break;
- case ItemTypeTouchLabel: {
- key = @"TouchLabel";
- }
- break;
- default: {
- self.template_data = nil;
- }
- break;
- }
-
- self.template_data = [[template objectForKey:key] mutableCopy];
- [self.template_data removeObjectForKey:@"size&position"];
-
- self.data = data;
-
- [self.tableView reloadData];
- }
- - (NSDictionary *)itemForSection:(NSInteger)section {
- NSString *section_key = [self.template_data.allKeys objectAtIndex:section];
- NSDictionary *section_dic = [self.template_data objectForKey:section_key];
- return section_dic;
- }
- - (NSDictionary *)cellItemForIndexPath:(NSIndexPath *)indexPath {
-
- NSDictionary *section_dic = [self itemForSection:indexPath.section];
- NSString *row_key = [section_dic.allKeys objectAtIndex:indexPath.row];
- NSDictionary *row_dic = [section_dic objectForKey:row_key];
-
- return row_dic;
- }
- #pragma mark - DataSource & Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- if (self.template_data) {
- return self.template_data.allKeys.count;
- }
- return 0;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-
- NSDictionary *section_dic = [self itemForSection:section];
- return section_dic.allKeys.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- NSDictionary *item = [self cellItemForIndexPath:indexPath];
- NSInteger type = [[item objectForKey:@"type"] integerValue];
- NSString *key = [item objectForKey:@"key"];
- NSString *title = [item objectForKey:@"title"];
- BOOL disable = [[item objectForKey:@"disable"] boolValue];
-
- __weak typeof(self) weakSelf = self;
- switch (type) {
- case CellTypeTextField: {
- AttributeTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textfield_cell_identifier" forIndexPath:indexPath];
- NSString *value = nil;
- if (self.check_option) {
- value = [item objectForKey:@"value"];
- if (value == nil) {
- NSMutableArray* cadedate = self.data[@"cadedate"];
- NSArray *item = cadedate[self.data_row];
- value = item[0][0];
- }
-
- } else {
-
- if([self.data[@"type"] isEqualToString:@"TouchLabel"])
- {
- value = [item objectForKey:@"value"];
- if (value == nil) {
- NSMutableDictionary* cadedate = [self.data[@"cadedate"] mutableCopy];
- NSMutableDictionary *item = [cadedate[[NSString stringWithFormat:@"val+%ld",self.data_row] ] mutableCopy];
- value = item[@"value"];
- }
- }
- else
- value = [self.data objectForKey:key];
-
-
- }
- [cell setTitle:title value:value forKey:key];
- cell.valueChangeBlock = ^(NSString *value, NSString *key) {
- if (weakSelf.check_option) {
- NSMutableArray* cadedate = [weakSelf.data[@"cadedate"] mutableCopy];
- NSMutableArray *item = [cadedate[weakSelf.data_row] mutableCopy];
- NSMutableArray *checkMarker = [[item objectAtIndex:0] mutableCopy];
-
- [checkMarker replaceObjectAtIndex:0 withObject:value];
- [item replaceObjectAtIndex:0 withObject:checkMarker];
- [cadedate replaceObjectAtIndex:weakSelf.data_row withObject:item];
- [weakSelf.data setObject:cadedate forKey:@"cadedate"];
- [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:weakSelf.data_row updateItemList:YES];
- } else {
- [weakSelf.data setValue:value forKey:key];
- [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:-1 updateItemList:NO];
- }
- };
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.userInteractionEnabled = !disable;
- return cell;
- }
- break;
- case CellTypeLabel: {
- AttributeLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label_cell_identifier" forIndexPath:indexPath];
- NSString *value = nil;
- if (self.check_option) {
- value = [item objectForKey:@"value"];
- if (value == nil) {
- NSMutableArray* cadedate = self.data[@"cadedate"];
- NSArray *item = cadedate[self.data_row];
- value = item[0][0];
- }
- } else {
- value = [self.data objectForKey:key];
- }
- [cell setTitle:title value:value forKey:key];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.userInteractionEnabled = !disable;
- return cell;
- }
- break;
- case CellTypeSwitch: {
- AttributeSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switch_cell_identifier" forIndexPath:indexPath];
- BOOL value = nil;
- if (self.check_option) {
- if ([item objectForKey:@"value"] == nil) {
- NSMutableArray* cadedate = self.data[@"cadedate"];
- NSArray *item = cadedate[self.data_row];
- value = item[0][0];
- } else {
- value = [[item objectForKey:@"value"] boolValue];
- }
- } else {
- value = [[self.data objectForKey:key] boolValue];
- }
- [cell setTitle:title value:value forKey:key];
- cell.valueChangeBlock = ^(BOOL value, NSString *key) {
-
- if (weakSelf.check_option) {
- NSMutableArray* cadedate = [weakSelf.data[@"cadedate"] mutableCopy];
- NSMutableArray *item = [cadedate[weakSelf.data_row] mutableCopy];
- NSMutableArray *checkMarker = [[item objectAtIndex:0] mutableCopy];
-
- [checkMarker replaceObjectAtIndex:0 withObject:@(value)];
- [item replaceObjectAtIndex:0 withObject:checkMarker];
- [cadedate replaceObjectAtIndex:weakSelf.data_row withObject:item];
- [weakSelf.data setObject:cadedate forKey:@"cadedate"];
-
- [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:weakSelf.data_row updateItemList:YES];
- } else {
- [weakSelf.data setValue:@(value) forKey:key];
- [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:-1 updateItemList:NO];
- }
-
-
- };
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.userInteractionEnabled = !disable;
- return cell;
- }
- break;
-
- default:
- break;
- }
-
- return nil;
- }
- - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
-
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
- label.backgroundColor = [UIColor brownColor];
- NSString *title = [self.template_data.allKeys objectAtIndex:section];
- [label setText:title];
-
- return label;
- }
- @end
|