EnumSelectViewController.m 21 KB

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