EnumSelectViewController.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. //
  2. // CommonEditorEnumViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-7-18.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "EnumSelectViewController.h"
  9. #import "EnumSelectorCell.h"
  10. #import "RAUtils.h"
  11. #import "AppDelegate.h"
  12. #define DEF_CELL_HEIGHT 44
  13. //#define DEF_TABLE_HEIGHT 44
  14. //#define LINE_WIDTH 0
  15. //#define CELL_MARGIN 0
  16. #define LABEL_MARGIN 15
  17. @interface EnumSelectViewController ()
  18. @end
  19. @implementation EnumSelectViewController
  20. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  21. {
  22. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  23. if (self) {
  24. // Custom initialization
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. // self.automaticallyAdjustsScrollViewInsets = NO;
  32. // self.max_select = 0;
  33. self.navi_item.title = self.title;
  34. // self.navigationItem.title = self.title;
  35. // Do any additional setup after loading the view.
  36. self.auto_close = true;
  37. if(self.is_dialog)
  38. {
  39. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  40. style:UIBarButtonItemStylePlain
  41. target:self
  42. action:@selector( onCloseClick:)];
  43. // [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  44. self.navi_item.rightBarButtonItem = closeButton;
  45. }
  46. else if(self.sys_style)
  47. {
  48. }
  49. else
  50. {
  51. UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  52. style:UIBarButtonItemStylePlain
  53. target:self
  54. action:@selector( onBackClick:)];
  55. // [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onBackClick:)];
  56. self.navigationItem.leftBarButtonItem = backButton;
  57. }
  58. self.dirty = false;
  59. NSString *model = [UIDevice currentDevice].model;
  60. if ([model isEqualToString:@"iPhone"]) {
  61. } else {
  62. UILabel* titleLabel= [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200
  63. , 44)];
  64. titleLabel.text=self.title;
  65. // titleLabel.backgroundColor =[UIColor yellowColor];
  66. titleLabel.textAlignment=NSTextAlignmentCenter;
  67. //将搜索条放在一个UIView上
  68. UIView *searchView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1768, 44)];
  69. searchView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;
  70. titleLabel.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  71. titleLabel.center=searchView.center;
  72. UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(searchView.frame.size.width-200.0f-8.0f,0.0f,200.0f,44.0f)];
  73. searchBar.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
  74. searchBar.delegate = self;
  75. [searchBar setPlaceholder:@"filter"];
  76. //searchBar.barTintColor=[UIColor clearColor];
  77. searchBar.searchBarStyle=UISearchBarStyleMinimal;
  78. [searchView addSubview:titleLabel];
  79. [searchView addSubview:searchBar];
  80. self.navigationItem.titleView = searchView;
  81. }
  82. [[self navigationController] setNavigationBarHidden:self.hide_navibar animated:NO];
  83. }
  84. - (void)onBackClick:(UIButton *)sender {
  85. [self.navigationController popViewControllerAnimated:(false)];
  86. // [self.navigationController dismissViewControllerAnimated:true completion:^{
  87. // ;
  88. // }];
  89. }
  90. - (void)onCloseClick:(UIButton *)sender {
  91. [self dismissViewControllerAnimated:YES
  92. completion:^{
  93. }];
  94. }
  95. - (void)viewWillDisappear:(BOOL)animated
  96. {
  97. [super viewWillDisappear:animated];
  98. if(self.dirty == false)
  99. return;
  100. if (self.delegate && [self.delegate respondsToSelector:@selector(EnumValueChanged:indexPath:)]) {
  101. [self.delegate EnumValueChanged:self.cadedate indexPath:self.updatePosition];
  102. }
  103. if(self.returnValue)
  104. self.returnValue(self.cadedate);
  105. }
  106. - (void)didReceiveMemoryWarning
  107. {
  108. [super didReceiveMemoryWarning];
  109. // Dispose of any resources that can be recreated.
  110. }
  111. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  112. //{
  113. //
  114. // [self.tableEnum reloadData];
  115. ////
  116. //// UIApplication * app = [UIApplication sharedApplication];
  117. //// AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  118. //// [appDelegate didRotated];
  119. //
  120. //
  121. //
  122. //}
  123. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  124. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  125. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  126. // what ever you want to prepare
  127. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  128. [self.tableEnum reloadData];
  129. }];
  130. }
  131. #pragma mark - Table view data source
  132. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  133. {
  134. return 1;
  135. }
  136. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  137. {
  138. // if(self.canbeEmpty)
  139. // return [[self.cadedate valueForKey:@"count"] intValue]+1;
  140. // else
  141. if(self.keywords==nil || self.keywords.length==0)
  142. {
  143. self.filter_count = 0;
  144. return [[self.cadedate valueForKey:@"count"] intValue];
  145. }
  146. else
  147. {
  148. int count = 0;
  149. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  150. {
  151. NSDictionary* val_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]];
  152. NSString* value = [val_json valueForKey:@"value"];
  153. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  154. if(location!= NSNotFound)
  155. count++;
  156. }
  157. self.filter_count = count;
  158. return count;
  159. }
  160. }
  161. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
  162. {
  163. NSDictionary* val_json =nil;
  164. if(self.keywords==nil || self.keywords.length==0)
  165. {
  166. val_json=[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]];
  167. }
  168. else
  169. {
  170. int count = 0;
  171. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  172. {
  173. if(count>= self.filter_count)
  174. break;
  175. NSDictionary* search_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]];
  176. NSString* value = [search_json valueForKey:@"value"];
  177. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  178. if(location!= NSNotFound)
  179. count++;
  180. if(count-1==indexPath.row)
  181. val_json=search_json;
  182. }
  183. }
  184. NSString* value = [val_json valueForKey:@"value"];
  185. float width = tableView.frame.size.width;
  186. width-=40;
  187. CGSize constraintkey = CGSizeMake(width-2*LABEL_MARGIN, 10.0f);//key label width is 40% cell width;
  188. // CGSize constraintval = CGSizeMake(width*0.6-2*LABEL_MARGIN, 20000.0f);//val label width is 60% cell width;
  189. // NSDictionary* item = [self.content get_item_seg:indexPath.section row:indexPath.row];
  190. // NSString* key =[self.content_data valueForKey:@"order_info"];
  191. // [cell.keyLabel sizeToFit];
  192. // NSString* val=[item_json valueForKey:@"val"] ;
  193. // if([val isEqual:[NSNull null]])
  194. // val=@"";
  195. // if(val==nil)
  196. // val=@"";
  197. // if([val isEqualToString:@"null"])
  198. // val=@"";
  199. CGRect frame;
  200. frame.size = constraintkey;
  201. frame.origin.x=0;
  202. frame.origin.y=0;
  203. RTLabel* rtlabel = [[RTLabel alloc] initWithFrame:frame];
  204. // rtlabel.lineSpacing = 10;
  205. // [rtlabel setLineBreakMode: RTTextLineBreakModeWordWrapping];
  206. // rtlabel.lineSpacing = 20.0;
  207. [rtlabel setText: value];
  208. CGSize optimumSize = [rtlabel optimumSize];
  209. // DebugLog(@"%@",key);
  210. // [rtlabel sizeThatFits:constraintkey];
  211. // rtlabel frameHeight:<#(CTFrameRef)#>
  212. // rtlabel
  213. // [rtlabel sizeToFit];
  214. //CGSize newsize= rtlabel.frame.size;
  215. // CGSize sizeval=rtlabel.optimumSize;
  216. // CGSize sizekey = [key sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  217. // CGSize sizeval = [val sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:constraintval lineBreakMode:NSLineBreakByWordWrapping];
  218. float height = optimumSize.height;
  219. height = MAX(height+2*LABEL_MARGIN, DEF_CELL_HEIGHT);
  220. return height;
  221. }
  222. //- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  223. //{
  224. // return UITableViewCellEditingStyleNone;
  225. //}
  226. //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  227. //{
  228. // //如果两次点击的时间间隔小于1秒,则断定为双击事件
  229. //
  230. // if(tableView.editing==false)
  231. // return;
  232. //
  233. //
  234. // if([tableView numberOfRowsInSection:indexPath.section]<=1)
  235. // return;
  236. // // int between = 1;
  237. // NSUInteger curr = [[NSDate date] timeIntervalSince1970];
  238. //
  239. // if(indexPath.row==self.lasttap.row&&indexPath.section==self.lasttap.section)
  240. // {
  241. // if (curr-self.taptime<1) {
  242. // DebugLog(@"double click!");
  243. // self.lasttap =nil;
  244. //
  245. // NSMutableArray * from;
  246. // NSMutableArray * to ;
  247. // int section;
  248. // if(indexPath.section==0)
  249. // {
  250. // from = self.displayfields;
  251. // to = self.hidefields;
  252. // section = 1;
  253. // }
  254. // else
  255. // {
  256. //
  257. // from = self.hidefields;
  258. // to = self.displayfields;
  259. // section = 0;
  260. // }
  261. // NSIndexPath * toIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
  262. // // NSIndexPath * headerIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
  263. // // NSIndexPath * headerIndexPath1 = toIndexPath;
  264. //
  265. // id content=from[indexPath.row];
  266. // [tableView beginUpdates];
  267. // [from removeObjectAtIndex:indexPath.row];
  268. // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  269. //
  270. // [to insertObject:content atIndex:0];
  271. // [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:toIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  272. // [tableView endUpdates];
  273. //
  274. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  275. // // [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:headerIndexPath1] withRowAnimation:UITableViewRowAnimationFade];
  276. // [tableView reloadData];
  277. //
  278. //
  279. // }
  280. // self.taptime = curr;
  281. //
  282. //
  283. // }
  284. // else
  285. // {
  286. // self.taptime = curr;
  287. // self.lasttap = indexPath;
  288. // }
  289. //}
  290. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  291. {
  292. NSDictionary* val_json =nil;
  293. if(self.keywords==nil || self.keywords.length==0)
  294. {
  295. val_json=[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]];
  296. }
  297. else
  298. {
  299. int count = 0;
  300. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  301. {
  302. if(count>= self.filter_count)
  303. break;
  304. NSDictionary* search_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]];
  305. NSString* value = [search_json valueForKey:@"value"];
  306. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  307. if(location!= NSNotFound)
  308. count++;
  309. if(count-1==indexPath.row)
  310. {
  311. val_json=search_json;
  312. break;
  313. }
  314. }
  315. }
  316. NSString* CellIdentifier = @"EnumSelectorCell";
  317. EnumSelectorCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  318. // NSDictionary* val_json = [self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]];
  319. NSString* value = [val_json valueForKey:@"value"];
  320. // cell.rtLabel.lineSpacing=10;
  321. cell.rtLabel.text = value;
  322. // cell.labelValue.text = value;
  323. int check = [[val_json valueForKey:@"check" ] intValue];
  324. if(check==1)
  325. {
  326. // cell.img_checkmark.hidden=false;
  327. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  328. }
  329. else
  330. {
  331. cell.accessoryType = UITableViewCellAccessoryNone;
  332. // cell.img_checkmark.hidden=true;
  333. }
  334. return cell;
  335. }
  336. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  337. {
  338. EnumSelectorCell* cell = (EnumSelectorCell*)[tableView cellForRowAtIndexPath:indexPath];
  339. cell.selected = false;
  340. self.dirty = true;
  341. if(self.single_select)
  342. {
  343. if(self.keywords==nil || self.keywords.length==0)
  344. {
  345. for(int i=0;i<[[self.cadedate valueForKey:@"count"] intValue];i++)
  346. {
  347. NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i]] mutableCopy];
  348. if(i==indexPath.row)
  349. {
  350. if ([val_json[@"check"] isEqual:@"1"])
  351. {
  352. //选中当前选中项时,不设脏状态
  353. self.dirty=false;
  354. }
  355. [val_json setValue:@"1" forKey:@"check"];
  356. [val_json setValue:@1 forKey:@"check1"];
  357. }
  358. else
  359. [val_json setValue:@"0" forKey:@"check"];
  360. [self.cadedate setObject:val_json forKey:[NSString stringWithFormat:@"val_%d",i]];
  361. }
  362. }
  363. else
  364. {
  365. NSMutableDictionary* val_json =nil;
  366. int count = 0;
  367. for (int cc=0; cc<[[self.cadedate valueForKey:@"count"] intValue]; cc++)
  368. {
  369. if(count>= self.filter_count)
  370. break;
  371. NSMutableDictionary* search_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",cc ]] mutableCopy];
  372. NSString* value = [search_json valueForKey:@"value"];
  373. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  374. if(location!= NSNotFound)
  375. count++;
  376. if(count-1==indexPath.row)
  377. val_json=search_json;
  378. }
  379. for (int sc=0;sc<[[self.cadedate valueForKey:@"count"] intValue];sc++)
  380. {
  381. NSMutableDictionary* sc_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",sc ]] mutableCopy];
  382. if ([[sc_json valueForKey:@"value" ] isEqual:[val_json valueForKey:@"value"]] )
  383. [sc_json setValue:@"1" forKey:@"check"];
  384. else
  385. [sc_json setValue:@"0" forKey:@"check"];
  386. [self.cadedate setObject:sc_json forKey:[NSString stringWithFormat:@"val_%d",sc]];
  387. }
  388. }
  389. if(self.auto_close)
  390. {
  391. if(self.navigationController!=nil)
  392. [self.navigationController popViewControllerAnimated:YES];
  393. // [self.navigationController dismissViewControllerAnimated:YES
  394. // completion:^{
  395. //
  396. //
  397. // }];
  398. else
  399. [self dismissViewControllerAnimated:YES
  400. completion:^{
  401. }];
  402. }
  403. }
  404. else
  405. {
  406. NSMutableDictionary* val_json =nil;
  407. if(self.keywords==nil || self.keywords.length==0)
  408. {
  409. val_json=[[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row ]] mutableCopy];
  410. }
  411. else
  412. {
  413. int count = 0;
  414. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  415. {
  416. if(count>= self.filter_count)
  417. break;
  418. NSMutableDictionary* search_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]] mutableCopy];
  419. NSString* value = [search_json valueForKey:@"value"];
  420. NSUInteger location=[[value uppercaseString] rangeOfString:self.keywords].location;
  421. if(location!= NSNotFound)
  422. count++;
  423. if(count-1==indexPath.row)
  424. val_json=search_json;
  425. }
  426. }
  427. // NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row]] mutableCopy];
  428. int check = [[val_json valueForKey:@"check"] intValue];
  429. if(check==1)
  430. [val_json setValue:@"0" forKey:@"check"];
  431. else
  432. {
  433. if([self check_count]>=self.max_select && self.max_select>0)
  434. {
  435. [RAUtils message_box:@"Max count reached" message:[NSString stringWithFormat:@"%d items at most for this field.",self.max_select ] completion:nil];
  436. return;
  437. }
  438. [val_json setValue:@"1" forKey:@"check"];
  439. }
  440. [self.cadedate setObject:val_json forKey:[NSString stringWithFormat:@"val_%ld",(long)indexPath.row]];
  441. }
  442. [self.tableEnum reloadData];
  443. }
  444. -(int) check_count
  445. {
  446. int check_count=0;
  447. // int count = 0;
  448. for (int i=0; i<[[self.cadedate valueForKey:@"count"] intValue]; i++)
  449. {
  450. NSMutableDictionary* val_json = [[self.cadedate objectForKey:[NSString stringWithFormat:@"val_%d",i ]] mutableCopy];
  451. int check = [[val_json valueForKey:@"check"] intValue];
  452. if(check==1)
  453. check_count++;
  454. }
  455. return check_count;
  456. }
  457. #pragma mark - searchBar delegate;
  458. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  459. {
  460. self.keywords=[searchBar.text uppercaseString];
  461. [self.tableEnum reloadData];
  462. // self.offset = 0;
  463. // [self.content_data removeAllObjects];
  464. // [self loadpage];
  465. // DebugLog(@"search");
  466. }
  467. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  468. {
  469. if([searchText isEqualToString:@""] && !self.reset)
  470. {
  471. self.keywords=nil;
  472. [self.tableEnum reloadData];
  473. self.reset=true;
  474. }
  475. else
  476. self.reset = false;
  477. }
  478. /*
  479. #pragma mark - Navigation
  480. // In a storyboard-based application, you will often want to do a little preparation before navigation
  481. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  482. {
  483. // Get the new view controller using [segue destinationViewController].
  484. // Pass the selected object to the new view controller.
  485. }
  486. */
  487. @end