AttributeTableDataSourceAndDelegate.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //
  2. // AttributeTableDataSource.m
  3. // Granite Expo eSign
  4. //
  5. // Created by Jack on 2017/10/11.
  6. // Copyright © 2017年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "AttributeTableDataSourceAndDelegate.h"
  9. #import "AttributeTextFieldCell.h"
  10. #import "AttributeLabelCell.h"
  11. #import "AttributeSwitchCell.h"
  12. #import "DocumentTemplateViewController.h"
  13. typedef enum {
  14. CellTypeTextField = 0,
  15. CellTypeLabel = 1,
  16. CellTypeSwitch = 2
  17. }CellType;
  18. @interface AttributeTableDataSourceAndDelegate ()
  19. @property (nonatomic,weak) DocumentTemplateViewController *table_vc;
  20. @property (nonatomic,weak) UITableView *tableView;
  21. @property (nonatomic,strong) NSMutableDictionary *template_data;
  22. @property (nonatomic,strong) NSMutableDictionary *data;
  23. @property (nonatomic,assign) NSInteger data_index;
  24. @property (nonatomic,assign) NSInteger data_row;
  25. @property (nonatomic,assign) BOOL check_option;
  26. @end
  27. @implementation AttributeTableDataSourceAndDelegate
  28. - (instancetype)initWithTableView:(UITableView *)tableView Controller:(DocumentTemplateViewController *)viewController {
  29. if (self = [super init]) {
  30. _tableView = tableView;
  31. _table_vc = viewController;
  32. _tableView.dataSource = self;
  33. _tableView.delegate = self;
  34. }
  35. return self;
  36. }
  37. - (void)changeListItemData:(NSMutableDictionary *)data forSection:(NSInteger)section Row:(NSInteger)row {
  38. self.check_option = NO;
  39. [self.tableView endEditing:YES];
  40. self.data_index = section;
  41. self.data_row = row;
  42. NSString *path = [[NSBundle mainBundle] pathForResource:@"attribute_template.json" ofType:nil];
  43. NSData *json_data = [NSData dataWithContentsOfFile:path];
  44. NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
  45. self.template_data = [[template objectForKey:@"List_Item"] mutableCopy];
  46. [self.template_data removeObjectForKey:@"size&position"];
  47. self.data = data;
  48. [self.tableView reloadData];
  49. }
  50. - (void)changeCheckOptionData:(NSMutableDictionary *)data forSection:(NSInteger)section Row:(NSInteger)row {
  51. self.check_option = YES;
  52. [self.tableView endEditing:YES];
  53. self.data_index = section;
  54. self.data_row = row;
  55. NSString *path = [[NSBundle mainBundle] pathForResource:@"attribute_template.json" ofType:nil];
  56. NSData *json_data = [NSData dataWithContentsOfFile:path];
  57. NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
  58. self.template_data = [[template objectForKey:@"Check_Option"] mutableCopy];
  59. [self.template_data removeObjectForKey:@"size&position"];
  60. self.data = data;
  61. [self.tableView reloadData];
  62. }
  63. - (void)changeData:(NSMutableDictionary *)data type:(ItemType)type forIndex:(NSInteger)index {
  64. self.check_option = NO;
  65. [self.tableView endEditing:YES]; // 正在编辑状态中,选择其他Item导致编辑信息写入新的Item
  66. if (data == nil || type == ItemTypeNone) {
  67. self.data = nil;
  68. self.template_data = nil;
  69. [self.tableView reloadData];
  70. return;
  71. }
  72. self.data_index = index;
  73. NSString *path = [[NSBundle mainBundle] pathForResource:@"attribute_template.json" ofType:nil];
  74. NSData *json_data = [NSData dataWithContentsOfFile:path];
  75. NSDictionary *template = [NSJSONSerialization JSONObjectWithData:json_data options:NSJSONReadingMutableLeaves error:nil];
  76. NSString *key = nil;
  77. switch (type) {
  78. case ItemTypeTextView: {
  79. key = @"TextView";
  80. }
  81. break;
  82. case ItemTypeSignature: {
  83. key = @"Signature";
  84. }
  85. break;
  86. case ItemTypeImage: {
  87. key = @"Image";
  88. }
  89. break;
  90. case ItemTypeIgnore: {
  91. key = @"Ignore";
  92. }
  93. break;
  94. case ItemTypeButton: {
  95. key = @"Button";
  96. }
  97. break;
  98. case ItemTypeCheck: {
  99. __block BOOL show_option = NO;
  100. // NSMutableArray* cadedate = data[@"cadedate"];
  101. // [cadedate enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  102. // NSArray* item = (NSArray *)obj;
  103. // NSArray* checkmarker = item[1];
  104. // if (checkmarker.count != 0) {
  105. // show_option = YES;
  106. // } else {
  107. // show_option = NO;
  108. // *stop = YES;
  109. // }
  110. // }];
  111. if ([data objectForKey:@"marker_size"] != nil) {
  112. show_option = YES;
  113. } else {
  114. show_option = NO;
  115. }
  116. if (show_option) {
  117. key = @"Check_0";
  118. } else {
  119. key = @"Check_1";
  120. }
  121. }
  122. break;
  123. case ItemTypeLabel: {
  124. key = @"Label";
  125. }
  126. break;
  127. case ItemTypeDatePicker: {
  128. key = @"DatePicker";
  129. }
  130. break;
  131. case ItemTypeTouchLabel: {
  132. key = @"TouchLabel";
  133. }
  134. break;
  135. default: {
  136. self.template_data = nil;
  137. }
  138. break;
  139. }
  140. self.template_data = [[template objectForKey:key] mutableCopy];
  141. [self.template_data removeObjectForKey:@"size&position"];
  142. self.data = data;
  143. [self.tableView reloadData];
  144. }
  145. - (NSDictionary *)itemForSection:(NSInteger)section {
  146. NSString *section_key = [self.template_data.allKeys objectAtIndex:section];
  147. NSDictionary *section_dic = [self.template_data objectForKey:section_key];
  148. return section_dic;
  149. }
  150. - (NSDictionary *)cellItemForIndexPath:(NSIndexPath *)indexPath {
  151. NSDictionary *section_dic = [self itemForSection:indexPath.section];
  152. NSString *row_key = [section_dic.allKeys objectAtIndex:indexPath.row];
  153. NSDictionary *row_dic = [section_dic objectForKey:row_key];
  154. return row_dic;
  155. }
  156. #pragma mark - DataSource & Delegate
  157. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  158. if (self.template_data) {
  159. return self.template_data.allKeys.count;
  160. }
  161. return 0;
  162. }
  163. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  164. NSDictionary *section_dic = [self itemForSection:section];
  165. return section_dic.allKeys.count;
  166. }
  167. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  168. NSDictionary *item = [self cellItemForIndexPath:indexPath];
  169. NSInteger type = [[item objectForKey:@"type"] integerValue];
  170. NSString *key = [item objectForKey:@"key"];
  171. NSString *title = [item objectForKey:@"title"];
  172. BOOL disable = [[item objectForKey:@"disable"] boolValue];
  173. __weak typeof(self) weakSelf = self;
  174. switch (type) {
  175. case CellTypeTextField: {
  176. AttributeTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textfield_cell_identifier" forIndexPath:indexPath];
  177. NSString *value = nil;
  178. if (self.check_option) {
  179. value = [item objectForKey:@"value"];
  180. if (value == nil) {
  181. NSMutableArray* cadedate = self.data[@"cadedate"];
  182. NSArray *item = cadedate[self.data_row];
  183. value = item[0][0];
  184. }
  185. } else {
  186. if([self.data[@"type"] isEqualToString:@"TouchLabel"])
  187. {
  188. value = [item objectForKey:@"value"];
  189. if (value == nil) {
  190. NSMutableDictionary* cadedate = [self.data[@"cadedate"] mutableCopy];
  191. NSMutableDictionary *item = [cadedate[[NSString stringWithFormat:@"val+%ld",self.data_row] ] mutableCopy];
  192. value = item[@"value"];
  193. }
  194. }
  195. else
  196. value = [self.data objectForKey:key];
  197. }
  198. [cell setTitle:title value:value forKey:key];
  199. cell.valueChangeBlock = ^(NSString *value, NSString *key) {
  200. if (weakSelf.check_option) {
  201. NSMutableArray* cadedate = [weakSelf.data[@"cadedate"] mutableCopy];
  202. NSMutableArray *item = [cadedate[weakSelf.data_row] mutableCopy];
  203. NSMutableArray *checkMarker = [[item objectAtIndex:0] mutableCopy];
  204. [checkMarker replaceObjectAtIndex:0 withObject:value];
  205. [item replaceObjectAtIndex:0 withObject:checkMarker];
  206. [cadedate replaceObjectAtIndex:weakSelf.data_row withObject:item];
  207. [weakSelf.data setObject:cadedate forKey:@"cadedate"];
  208. [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:weakSelf.data_row updateItemList:YES];
  209. } else {
  210. [weakSelf.data setValue:value forKey:key];
  211. [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:-1 updateItemList:NO];
  212. }
  213. };
  214. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  215. cell.userInteractionEnabled = !disable;
  216. return cell;
  217. }
  218. break;
  219. case CellTypeLabel: {
  220. AttributeLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label_cell_identifier" forIndexPath:indexPath];
  221. NSString *value = nil;
  222. if (self.check_option) {
  223. value = [item objectForKey:@"value"];
  224. if (value == nil) {
  225. NSMutableArray* cadedate = self.data[@"cadedate"];
  226. NSArray *item = cadedate[self.data_row];
  227. value = item[0][0];
  228. }
  229. } else {
  230. value = [self.data objectForKey:key];
  231. }
  232. [cell setTitle:title value:value forKey:key];
  233. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  234. cell.userInteractionEnabled = !disable;
  235. return cell;
  236. }
  237. break;
  238. case CellTypeSwitch: {
  239. AttributeSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switch_cell_identifier" forIndexPath:indexPath];
  240. BOOL value = nil;
  241. if (self.check_option) {
  242. if ([item objectForKey:@"value"] == nil) {
  243. NSMutableArray* cadedate = self.data[@"cadedate"];
  244. NSArray *item = cadedate[self.data_row];
  245. value = item[0][0];
  246. } else {
  247. value = [[item objectForKey:@"value"] boolValue];
  248. }
  249. } else {
  250. value = [[self.data objectForKey:key] boolValue];
  251. }
  252. [cell setTitle:title value:value forKey:key];
  253. cell.valueChangeBlock = ^(BOOL value, NSString *key) {
  254. if (weakSelf.check_option) {
  255. NSMutableArray* cadedate = [weakSelf.data[@"cadedate"] mutableCopy];
  256. NSMutableArray *item = [cadedate[weakSelf.data_row] mutableCopy];
  257. NSMutableArray *checkMarker = [[item objectAtIndex:0] mutableCopy];
  258. [checkMarker replaceObjectAtIndex:0 withObject:@(value)];
  259. [item replaceObjectAtIndex:0 withObject:checkMarker];
  260. [cadedate replaceObjectAtIndex:weakSelf.data_row withObject:item];
  261. [weakSelf.data setObject:cadedate forKey:@"cadedate"];
  262. [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:weakSelf.data_row updateItemList:YES];
  263. } else {
  264. [weakSelf.data setValue:@(value) forKey:key];
  265. [weakSelf.table_vc updatePageControlProperty:weakSelf.data forIndex:self.data_index Row:-1 updateItemList:NO];
  266. }
  267. };
  268. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  269. cell.userInteractionEnabled = !disable;
  270. return cell;
  271. }
  272. break;
  273. default:
  274. break;
  275. }
  276. return nil;
  277. }
  278. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  279. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
  280. label.backgroundColor = [UIColor brownColor];
  281. NSString *title = [self.template_data.allKeys objectAtIndex:section];
  282. [label setText:title];
  283. return label;
  284. }
  285. @end