EnumSelectViewController.m 21 KB

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