EnumSelectViewController.m 20 KB

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