SearchViewController.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. //
  2. // SearchViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-3-3.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "const.h"
  9. #import "SearchViewController.h"
  10. #import "SearchTableAdapterJson.h"
  11. //#import "DB_Field.h"
  12. #import "TableCellEdit.h"
  13. #import "TableCellDate.h"
  14. #import "TableCellBool.h"
  15. #import "MyAutocompleteView.h"
  16. //#import "AMResultViewController.h"
  17. #import "ResultViewController.h"
  18. @interface SearchViewController () <UISearchBarDelegate>
  19. @property (strong,nonatomic) SearchTableAdapterJson* adapter ;
  20. @property (strong, nonatomic) IBOutlet UISearchBar *searchaBar;
  21. @property (strong, nonatomic) IBOutlet NSLayoutConstraint *tableBottomConstraint;
  22. @property (nonatomic,assign) BOOL showTable;
  23. @property (nonatomic,strong) NSIndexPath *editingIndexPath;
  24. @property (strong, nonatomic) IBOutlet NSLayoutConstraint *toolBarheightConstraint;
  25. @property (strong, nonatomic) IBOutlet UIToolbar *searchToolbar;
  26. @end
  27. @implementation SearchViewController
  28. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  29. {
  30. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  31. if (self) {
  32. // Custom initialization
  33. }
  34. return self;
  35. }
  36. - (void)viewDidLoad
  37. {
  38. [super viewDidLoad];
  39. self.ioffset = 0;
  40. NSString* title=@"";
  41. if([self.subtype isEqualToString:@"aci"])
  42. title = @"ACI Search";
  43. else if([self.subtype isEqualToString:@"isf"])
  44. title = @"ISF Search";
  45. else if([self.subtype isEqualToString:@"acem1"])
  46. title = @"ACE-M1 Search";
  47. else if([self.subtype isEqualToString:@"emanifest"])
  48. title = @"eManifest Search";
  49. else if([self.subtype isEqualToString:@"customer"])
  50. title = @"Customer Search";
  51. self.navigationItem.title = title;
  52. // UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
  53. // backButton.title = @"Back";
  54. // self.navigationItem.backBarButtonItem = backButton;
  55. self.dirty = true;
  56. DebugLog(@"viewDidLoad");
  57. self.table.tableFooterView = [UIView new];
  58. self.searchaBar.delegate = self;
  59. // 2019.1.8 使得Document显示searchToolbar
  60. // if ([self.function_name isEqualToString:@"Download Document"]) {
  61. // self.toolBarheightConstraint.constant = 0;
  62. // [self.view layoutIfNeeded];
  63. // self.searchToolbar.hidden = YES;
  64. // self.showTable = YES;
  65. // }
  66. // UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  67. // backItem.title = @"";
  68. // self.navigationItem.backBarButtonItem = backItem;
  69. }
  70. - (void)viewWillDisappear:(BOOL)animated {
  71. [super viewWillDisappear:animated];
  72. [[NSNotificationCenter defaultCenter] removeObserver:self];
  73. }
  74. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  75. // NSTimeInterval duration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  76. // CGRect begin = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  77. CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  78. CGFloat endY = CGRectGetMinY(end);
  79. CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
  80. CGFloat offset = screenHeight - endY;
  81. CGFloat tableHeightoffset = offset - 44;
  82. if (tableHeightoffset > 0) {
  83. self.tableBottomConstraint.constant = offset;
  84. if (self.editingIndexPath) {
  85. [self.table scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  86. }
  87. } else {
  88. self.tableBottomConstraint.constant = offset;
  89. }
  90. [self.view layoutIfNeeded];
  91. }
  92. - (IBAction)searchTableExpandableBtnClick:(UIButton *)sender {
  93. sender.selected = !sender.selected;
  94. self.showTable = !self.showTable;
  95. if (self.showTable) {
  96. self.adapter=[[SearchTableAdapterJson alloc] init];
  97. [self.adapter initfields:@"search" subtype:self.subtype];
  98. } else {
  99. self.adapter = nil;
  100. }
  101. [self.table reloadData];
  102. }
  103. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  104. [self SearchBtnOnClick:nil];
  105. }
  106. - (IBAction)onBoolitemClick:(UIButton *)sender {
  107. DebugLog(@"onBoolitemClick");
  108. UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
  109. // DebugLog(@"reuseid:%@",cell.reuseIdentifier);
  110. // // UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
  111. NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  112. if(indexPath==nil)
  113. {//for ios7
  114. cell = (UITableViewCell *) sender.superview.superview.superview;
  115. indexPath = [self.table indexPathForCell:cell];
  116. }
  117. // NSMutableDictionary * field =[_adapter get_item:[indexPath row]];
  118. // DebugLog(@"textFieldShouldBeginEditing row=%ld type=%@",(long)[indexPath row], [field valueForKey:@"type"]);
  119. // if(![[field valueForKey:@"type"] isEqualToString:@"bool"])
  120. // return YES;
  121. NSString *title = NSLocalizedString(@"select_val", nil);//UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"Select value" : @"Select value";
  122. // UIActionSheet *selBool = [[UIActionSheet alloc] initWithTitle:title
  123. // delegate:self
  124. // cancelButtonTitle:NSLocalizedString(@"no", nil)
  125. // destructiveButtonTitle:NSLocalizedString(@"clear", nil)
  126. // otherButtonTitles:NSLocalizedString(@"yes", nil),
  127. // nil];
  128. // selBool.tag = 1;
  129. // //dateDialog.actionSheetStyle = self.navigationController.navigationBar.barStyle;
  130. // [selBool showInView:self.view];
  131. self.boolBtntmp = sender;
  132. // UIDatePicker *datePicker = [[UIDatePicker alloc] init];
  133. // datePicker.tag = 101;
  134. // datePicker.datePickerMode=UIDatePickerModeDate;
  135. // [dateDialog addSubview:datePicker];
  136. // NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  137. // 此处编写弹出日期选择器的代码。
  138. // self.textFieldtmp=textField;
  139. __weak typeof(self) weakSelf = self;
  140. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  141. UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"no", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  142. TableCellBool *cell = (TableCellBool *)[[weakSelf.boolBtntmp superview] superview];
  143. NSIndexPath *indexPath = [weakSelf.table indexPathForCell:cell];
  144. if(indexPath==nil)
  145. {//for ios7
  146. cell = (TableCellBool *) weakSelf.boolBtntmp.superview.superview.superview;
  147. indexPath = [weakSelf.table indexPathForCell:cell];
  148. }
  149. {
  150. [weakSelf.boolBtntmp setTitle:@"No" forState:UIControlStateNormal];
  151. NSMutableDictionary* item =[weakSelf.adapter get_item:[indexPath row]];
  152. [item setValue:@"false" forKey:@"value"];
  153. [weakSelf.adapter set_item:indexPath.row item:item];
  154. }
  155. }];
  156. UIAlertAction *clearAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"clear", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  157. TableCellBool *cell = (TableCellBool *)[[weakSelf.boolBtntmp superview] superview];
  158. NSIndexPath *indexPath = [weakSelf.table indexPathForCell:cell];
  159. if(indexPath==nil)
  160. {//for ios7
  161. cell = (TableCellBool *) weakSelf.boolBtntmp.superview.superview.superview;
  162. indexPath = [weakSelf.table indexPathForCell:cell];
  163. }
  164. {
  165. [self.boolBtntmp setTitle:@"Not set" forState:UIControlStateNormal];
  166. NSMutableDictionary* item =[weakSelf.adapter get_item:[indexPath row]];
  167. [item setValue:@"" forKey:@"value"];
  168. [weakSelf.adapter set_item:indexPath.row item:item];
  169. }
  170. }];
  171. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  172. TableCellBool *cell = (TableCellBool *)[[weakSelf.boolBtntmp superview] superview];
  173. NSIndexPath *indexPath = [weakSelf.table indexPathForCell:cell];
  174. if(indexPath==nil)
  175. {//for ios7
  176. cell = (TableCellBool *) weakSelf.boolBtntmp.superview.superview.superview;
  177. indexPath = [weakSelf.table indexPathForCell:cell];
  178. }
  179. {
  180. [weakSelf.boolBtntmp setTitle:@"Yes" forState:UIControlStateNormal];
  181. NSMutableDictionary* item =[weakSelf.adapter get_item:[indexPath row]];
  182. [item setValue:@"true" forKey:@"value"];
  183. [weakSelf.adapter set_item:indexPath.row item:item];
  184. }
  185. }];
  186. [alertVC addAction:yesAction];
  187. [alertVC addAction:noAction];
  188. [alertVC addAction:clearAction];
  189. [self presentViewController:alertVC animated:YES completion:nil];
  190. }
  191. - (void)viewWillAppear:(BOOL)animated
  192. {
  193. [super viewWillAppear:animated];
  194. DebugLog(@"viewWillAppear");
  195. if(self.dirty ==true)
  196. {
  197. _adapter=[[SearchTableAdapterJson alloc] init];
  198. [self.adapter initfields:@"search" subtype:self.subtype];
  199. [self.table reloadData];
  200. self.dirty=false;
  201. }
  202. [self.table reloadData];
  203. // 解决Items按钮灰色
  204. self.navigationItem.rightBarButtonItem.enabled = false;
  205. self.navigationItem.rightBarButtonItem.enabled = true;
  206. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  207. }
  208. //-(void) viewWillDisappear:(BOOL)animated
  209. //{
  210. // [[self view] endEditing:animated];
  211. //}
  212. - (IBAction)onCustomize:(UIBarButtonItem *)sender {
  213. CustomizeFieldViewController *ViewController = [[UIStoryboard storyboardWithName:@"Fields" bundle:nil] instantiateViewControllerWithIdentifier:@"CustomizeFieldViewController"];
  214. // ViewController.function_name = self.function_name;
  215. ViewController.type = @"search";
  216. ViewController.subtype = self.subtype;
  217. ViewController.behavior =BEHAVIOR_SEARCH;
  218. self.dirty = true;
  219. //ViewController.dirty = self.dirty;
  220. // ViewController.
  221. [self.navigationController pushViewController:ViewController animated:YES];
  222. }
  223. - (void)didReceiveMemoryWarning
  224. {
  225. [super didReceiveMemoryWarning];
  226. // Dispose of any resources that can be recreated.
  227. }
  228. #pragma mark - Table view data source
  229. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  230. {
  231. DebugLog(@"numberOfSectionsInTableView");
  232. return 1;
  233. }
  234. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  235. {
  236. if (self.showTable) {
  237. return [_adapter get_itemcount];
  238. } else {
  239. return 0;
  240. }
  241. }
  242. //- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  243. //
  244. //}
  245. //- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
  246. //{
  247. // [[self view] endEditing:YES];
  248. // return false;
  249. //}
  250. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  251. {
  252. NSMutableDictionary * field =[_adapter get_item:[indexPath row]];
  253. if([[field valueForKey:@"type"] isEqualToString:@"boolean"])
  254. {
  255. NSString *CellIdentifier = @"item_bool";
  256. TableCellBool *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  257. cell.anameLabel.text = [field valueForKey:@"aname"];
  258. NSString * val = [field valueForKey:@"value"];
  259. if(val.length==0)
  260. [cell.valButton setTitle:@"Not set" forState:UIControlStateNormal];
  261. else
  262. if([val isEqualToString:@"true"])
  263. [cell.valButton setTitle:@"Yes" forState:UIControlStateNormal];
  264. else
  265. [cell.valButton setTitle:@"No" forState:UIControlStateNormal];
  266. [cell.anameLabel sizeToFit];
  267. DebugLog(@"cellForRowAtIndexPath%@",[field valueForKey:@"aname"]);
  268. // Configure the cell...
  269. return cell;
  270. }
  271. else if([[field valueForKey:@"type"] isEqualToString:@"time"])
  272. {
  273. NSString *CellIdentifier = @"item_date";
  274. TableCellDate *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  275. cell.anameLabel.text = [field valueForKey:@"aname"];
  276. [cell.anameLabel sizeToFit];
  277. DebugLog(@"cellForRowAtIndexPath%@",[field valueForKey:@"aname"]);
  278. // Configure the cell...
  279. NSString* str_from= [field valueForKey:@"from"];
  280. NSString* str_to= [field valueForKey:@"to"];
  281. // if(str_from.length>0)
  282. cell.fromEdit.text = str_from;
  283. cell.toEdit.text = str_to;
  284. return cell;
  285. // static NSString *CellIdentifier = @"item_date";
  286. ////
  287. //// //注册要使用的cell
  288. //// [tableView registerClass:[TableCellDate class]
  289. //// forCellReuseIdentifier:CellIdentifier];
  290. //
  291. // //创建cell
  292. // TableCellDate *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  293. //
  294. // if(cell == nil)
  295. // {
  296. // cell = [[TableCellDate alloc] initWithStyle:UITableViewCellStyleValue1
  297. // reuseIdentifier:CellIdentifier];
  298. // }
  299. //
  300. // cell.anameLabel.text = [field valueForKey:@"aname"];
  301. // [cell.anameLabel sizeToFit];
  302. // DebugLog(@"cellForRowAtIndexPath%@",[field valueForKey:@"aname"]);
  303. // return cell;
  304. }
  305. else
  306. {
  307. NSString *CellIdentifier = @"item_edit";
  308. NSString * type = [field valueForKey:@"type"];
  309. TableCellEdit *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  310. cell.anameLabel.text = [field valueForKey:@"aname"];
  311. [cell.anameLabel sizeToFit];
  312. NSString * val = [field valueForKey:@"value"];
  313. if([type isEqualToString:@"int"])
  314. cell.valueEdit.keyboardType = UIKeyboardTypeNumberPad;
  315. else
  316. cell.valueEdit.keyboardType = UIKeyboardTypeDefault;
  317. cell.valueEdit.text = val;
  318. NSArray* array=nil;
  319. // NSArray* array = [ApexMobileDB get_searchhistory:[field valueForKey:@"name"]];
  320. // [self.autocompleteView.itemsSource put_predictions:array ];
  321. MyAutocompleteView* autocompleteView = [MyAutocompleteView autocompleteViewBindedTo:cell.valueEdit
  322. usingSource:[[MyAutocompleteItemsSource alloc]
  323. initWithMinimumCharactersToTrigger:1
  324. source:array
  325. ]
  326. cellFactory:[[MyAutocompletionCellFactory alloc]
  327. initWithCellTextColor:[UIColor blackColor]
  328. fontSize:14]
  329. // presentingIn:self
  330. parentView:self.table];
  331. autocompleteView.topMargin = 0;
  332. // self.autocompleteView.backgroundColor = [UIColor ui];
  333. autocompleteView.didAutocompleteWith = ^(MySuggestion* item)
  334. {
  335. NSLog(@"Autocompleted with: %@", item.completionText);
  336. };
  337. cell.autocompleteView = autocompleteView;
  338. // if(val.length==0)
  339. DebugLog(@"cellForRowAtIndexPath%@",[field valueForKey:@"aname"]);
  340. // Configure the cell...
  341. return cell;
  342. // static NSString *CellIdentifier = @"item_edit";
  343. //
  344. //// //注册要使用的cell
  345. //// [tableView registerClass:[TableCellEdit class]
  346. //// forCellReuseIdentifier:CellIdentifier];
  347. //
  348. // //创建cell
  349. // TableCellEdit *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  350. //
  351. // if(cell == nil)
  352. // {
  353. // cell = [[TableCellEdit alloc] initWithStyle:UITableViewCellStyleValue1
  354. // reuseIdentifier:CellIdentifier];
  355. // }
  356. //
  357. // cell.anameLabel.text = [field valueForKey:@"aname"];
  358. // [cell.anameLabel sizeToFit];
  359. // DebugLog(@"cellForRowAtIndexPath%@",[field valueForKey:@"aname"]);
  360. // return cell;
  361. }
  362. return nil;
  363. }
  364. - (IBAction)ClearBtnOnClick:(UIButton *)sender {
  365. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"attention", nil) message:NSLocalizedString(@"alert_title_confirm_clear", nil) preferredStyle:UIAlertControllerStyleAlert];
  366. __weak typeof(self) weakSelf = self;
  367. UIAlertAction *noAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"no", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  368. }];
  369. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"yes", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  370. [weakSelf.view endEditing:YES];
  371. weakSelf.adapter=[[SearchTableAdapterJson alloc] init];
  372. [weakSelf.adapter initfields:@"search" subtype:weakSelf.subtype];
  373. [weakSelf.table reloadData];
  374. }];
  375. [alertVC addAction:noAction];
  376. [alertVC addAction:yesAction];
  377. [self presentViewController:alertVC animated:YES completion:nil];
  378. }
  379. - (IBAction)SearchBtnOnClick:(UIButton *)sender {
  380. [self.textFieldtmp endEditing:true];
  381. // if([self.function_name isEqualToString:@"Document Download"])
  382. // resultViewController.fullrow_select =false;
  383. // else
  384. // resultViewController.fullrow_select=true;
  385. NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  386. for(int i=0;i<[_adapter get_itemcount];i++)
  387. {
  388. NSMutableDictionary * field =[_adapter get_item:i];
  389. if([[field valueForKey:@"type"] isEqualToString:@"time"])
  390. {
  391. NSString* str_from= [field valueForKey:@"from"];
  392. NSString* str_to= [field valueForKey:@"to"];
  393. if(str_from.length>0)
  394. [params setValue:str_from forKey:[NSString stringWithFormat:@"%@_from",[field valueForKey:@"name"]]];
  395. if(str_to.length>0)
  396. [params setValue:str_to forKey:[NSString stringWithFormat:@"%@_to",[field valueForKey:@"name"]]];
  397. }
  398. else
  399. {
  400. NSString* value =[field valueForKey:@"value"];
  401. if(value.length>0)
  402. {
  403. [params setValue:[field valueForKey:@"value"] forKey:[field valueForKey:@"name"]];
  404. if([[field valueForKey:@"type"] isEqualToString:@"string"] )
  405. {
  406. // [ApexMobileDB savehistory:[field valueForKey:@"name"] value:[field valueForKey:@"value"]];
  407. }
  408. }
  409. }
  410. }
  411. // [params setValue:self.function_name forKey:@"module_name"];
  412. [params setValue:self.searchaBar.text forKey:@"keyword"];
  413. params[@"sub_type"]=self.subtype;
  414. // after 2019.1.5
  415. ResultViewController *vc = [ResultViewController resultViewController:params];
  416. [self.navigationController pushViewController:vc animated:YES];
  417. // before 2019.1.5
  418. // AMResultViewController *resultVC = [[AMResultViewController alloc] initWithNibName:@"Result" bundle:nil];
  419. // resultVC.params = params;
  420. // [self.navigationController pushViewController:resultVC animated:YES];
  421. // AMResultViewController *resultVC = [[AMResultViewController alloc] initWithNibName:@"Result" bundle:nil];
  422. // [self.navigationController pushViewController:resultVC animated:YES];
  423. }
  424. //- (IBAction)onTouchDown:(UITextField *)sender {
  425. // DebugLog(@"onTouchDown");
  426. //}
  427. //
  428. //- (IBAction)dateEditOnClick:(UITextField *)sender forEvent:(UIEvent *)event {
  429. // DebugLog(@"dateEditOnClick");
  430. // UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
  431. // // DebugLog(@"reuseid:%@",cell.reuseIdentifier);
  432. // // // UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
  433. // NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  434. // NSMutableDictionary * field =[_adapter get_item:[indexPath row]];
  435. // if(![[field valueForKey:@"type"] isEqualToString:@"time"])
  436. // return ;
  437. //
  438. // NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n";
  439. // UIActionSheet *dateDialog = [[UIActionSheet alloc] initWithTitle:title
  440. // delegate:self
  441. // cancelButtonTitle:@"Cancel"
  442. // destructiveButtonTitle:@"Set"
  443. // otherButtonTitles:nil,
  444. // nil];
  445. // //dateDialog.actionSheetStyle = self.navigationController.navigationBar.barStyle;
  446. // [dateDialog showInView:self.view];
  447. // UIDatePicker *datePicker = [[UIDatePicker alloc] init];
  448. // datePicker.tag = 101;
  449. // datePicker.datePickerMode=UIDatePickerModeDate;
  450. // [dateDialog addSubview:datePicker];
  451. //
  452. //
  453. // // NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  454. // // 此处编写弹出日期选择器的代码。
  455. // self.textFieldtmp=sender;
  456. //
  457. //}
  458. - (BOOL)shouldAutorotate
  459. {
  460. return YES;
  461. }
  462. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  463. {
  464. return UIInterfaceOrientationMaskPortrait;
  465. }
  466. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  467. {
  468. return UIInterfaceOrientationPortrait;
  469. }
  470. #pragma mark - Text field delegate
  471. - (void)textFieldDidBeginEditing:(UITextField *)textField {
  472. // int fieldpos = [textField convertPoint:textField.bounds.origin toView:self.view].y+textField.frame.size.height;
  473. // fieldpos+=150 ;//autocomplete height;
  474. // int screenheight =self.view.frame.size.height;
  475. // int ioffset = fieldpos - (screenheight - 216);//216 -(self.view.frame.size.height-fieldpos);
  476. // if(ioffset>0)
  477. // {
  478. // self.ioffset = ioffset;
  479. // CGPoint offset = self.table.contentOffset;
  480. // offset.y+=ioffset;
  481. // DebugLog(@"table content offset%d",ioffset);
  482. // [self.table setContentOffset:offset animated:YES];
  483. // }
  484. UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
  485. NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  486. if(indexPath==nil)
  487. {//for ios7
  488. cell = (UITableViewCell *) textField.superview.superview.superview;
  489. indexPath = [self.table indexPathForCell:cell];
  490. }
  491. NSMutableDictionary * field =[_adapter get_item:[indexPath row]];
  492. DebugLog(@"%s row=%ld type=%@",__func__,(long)[indexPath row], [field valueForKey:@"type"]);
  493. if(![[field valueForKey:@"type"] isEqualToString:@"time"])
  494. {
  495. self.editingIndexPath = indexPath;
  496. self.textFieldtmp = textField;
  497. }
  498. }
  499. //- (BOOL)textFieldShouldReturn:(UITextField *)sender {
  500. // [self.table setContentOffset:CGPointMake(0, 0) animated:YES];
  501. // return YES;
  502. //}
  503. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  504. // if(self.ioffset>0)
  505. // {
  506. //
  507. // CGPoint offset = self.table.contentOffset;
  508. // offset.y-=self.ioffset;
  509. // [self.table setContentOffset:offset animated:YES];
  510. // self.ioffset = 0;
  511. // }
  512. //
  513. // [self.table setContentOffset:CGPointMake(0, 0) animated:YES];
  514. [textField resignFirstResponder];
  515. return YES;
  516. }
  517. - (void)textFieldDidEndEditing:(UITextField *)textField
  518. {
  519. DebugLog(@"textFieldDidEndEditing%@",textField.text);
  520. self.editingIndexPath = nil;
  521. UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
  522. NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  523. // if(indexPath==nil)
  524. // {//for ios7
  525. // cell = (UITableViewCell *) textField.superview.superview.superview;
  526. // indexPath = [self.table indexPathForCell:cell];
  527. // }
  528. // [self.table setContentOffset:CGPointMake(0, 0) animated:YES];
  529. NSMutableDictionary* item = [_adapter get_item:[indexPath row]];
  530. [item setValue:textField.text forKey:@"value"];
  531. [_adapter set_item:[indexPath row] item:item];
  532. self.textFieldtmp = nil;
  533. }
  534. //
  535. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  536. {
  537. // [self.table setContentOffset:CGPointMake(0, 70) animated:YES];
  538. DebugLog(@"textFieldShouldBeginEditing text=%@",textField.text);
  539. UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
  540. // DebugLog(@"reuseid:%@",cell.reuseIdentifier);
  541. // // UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
  542. NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  543. if(indexPath==nil)
  544. {//for ios7
  545. cell = (UITableViewCell *) textField.superview.superview.superview;
  546. indexPath = [self.table indexPathForCell:cell];
  547. }
  548. NSMutableDictionary * field =[_adapter get_item:[indexPath row]];
  549. DebugLog(@"textFieldShouldBeginEditing row=%ld type=%@",(long)[indexPath row], [field valueForKey:@"type"]);
  550. if(![[field valueForKey:@"type"] isEqualToString:@"time"])
  551. {
  552. self.editingIndexPath = indexPath;
  553. self.textFieldtmp = textField;
  554. return YES;
  555. }
  556. UIDatePicker *datePicker = [[UIDatePicker alloc] init];
  557. datePicker.tag = 101;
  558. datePicker.datePickerMode=UIDatePickerModeDate;
  559. NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n";
  560. // UIActionSheet *dateDialog = [[UIActionSheet alloc] initWithTitle:title
  561. // delegate:self
  562. // cancelButtonTitle:NSLocalizedString(@"cancel",nil)
  563. // destructiveButtonTitle:NSLocalizedString(@"clear",nil)
  564. // otherButtonTitles:NSLocalizedString(@"set",nil),
  565. // nil];
  566. // dateDialog.tag = 0;
  567. // //dateDialog.actionSheetStyle = self.navigationController.navigationBar.barStyle;
  568. // [dateDialog showInView:self.view];
  569. // [dateDialog addSubview:datePicker];
  570. __weak typeof(self) weakSelf = self;
  571. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  572. UIAlertAction *setAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"set",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  573. NSDate *date = [datePicker date];
  574. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  575. //设定时间格式,这里可以设置成自己需要的格式
  576. [dateFormatter setDateFormat:@"MM/dd/yyyy"];
  577. //用[NSDate date]可以获取系统当前时间
  578. self.textFieldtmp.text = [dateFormatter stringFromDate:date];
  579. UITableViewCell *cell = (UITableViewCell *)[[weakSelf.textFieldtmp superview] superview];
  580. NSIndexPath *indexPath = [weakSelf.table indexPathForCell:cell];
  581. if(indexPath==nil)
  582. {//for ios7
  583. cell = (UITableViewCell *) weakSelf.textFieldtmp.superview.superview.superview;
  584. indexPath = [weakSelf.table indexPathForCell:cell];
  585. }
  586. if([weakSelf.textFieldtmp.restorationIdentifier isEqualToString:@"edit_from"])
  587. {
  588. NSMutableDictionary* item = [weakSelf.adapter get_item:[indexPath row]];
  589. [item setValue:[dateFormatter stringFromDate:date] forKey:@"from"];
  590. [weakSelf.adapter set_item:indexPath.row item:item];
  591. }
  592. else
  593. {
  594. NSMutableDictionary* item = [weakSelf.adapter get_item:[indexPath row]];
  595. [item setValue:[dateFormatter stringFromDate:date] forKey:@"to"];
  596. [weakSelf.adapter set_item:indexPath.row item:item];
  597. }
  598. }];
  599. UIAlertAction *clearAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"clear",nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  600. weakSelf.textFieldtmp.text=nil;
  601. UITableViewCell *cell = (UITableViewCell *)[[weakSelf.textFieldtmp superview] superview];
  602. NSIndexPath *indexPath = [weakSelf.table indexPathForCell:cell];
  603. if(indexPath==nil)
  604. {//for ios7
  605. cell = (UITableViewCell *) weakSelf.textFieldtmp.superview.superview.superview;
  606. indexPath = [weakSelf.table indexPathForCell:cell];
  607. }
  608. if([weakSelf.textFieldtmp.restorationIdentifier isEqualToString:@"edit_from"])
  609. {
  610. NSMutableDictionary* item = [weakSelf.adapter get_item:[indexPath row]];
  611. [item setValue:@"" forKey:@"from"];
  612. [weakSelf.adapter set_item:indexPath.row item:item];
  613. }
  614. else
  615. {
  616. NSMutableDictionary* item = [weakSelf.adapter get_item:[indexPath row]];
  617. [item setValue:@"" forKey:@"to"];
  618. [weakSelf.adapter set_item:indexPath.row item:item];
  619. }
  620. }];
  621. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancel",nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  622. }];
  623. [alertVC.view addSubview:datePicker];
  624. [alertVC addAction:setAction];
  625. [alertVC addAction:clearAction];
  626. [alertVC addAction:cancelAction];
  627. [self presentViewController:alertVC animated:YES completion:nil];
  628. // NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  629. // 此处编写弹出日期选择器的代码。
  630. self.textFieldtmp=textField;
  631. //
  632. //
  633. // [UIView setAnimationDuration:5.0];
  634. // [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  635. // [UIView commitAnimations];
  636. //
  637. return NO;
  638. }
  639. //- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  640. //
  641. // if(actionSheet.tag==0)
  642. // {
  643. //
  644. // if(buttonIndex==actionSheet.cancelButtonIndex)
  645. // return;
  646. // if(buttonIndex==actionSheet.destructiveButtonIndex)
  647. // {
  648. // self.textFieldtmp.text=nil;
  649. // UITableViewCell *cell = (UITableViewCell *)[[self.textFieldtmp superview] superview];
  650. //
  651. // NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  652. // if(indexPath==nil)
  653. // {//for ios7
  654. // cell = (UITableViewCell *) self.textFieldtmp.superview.superview.superview;
  655. // indexPath = [self.table indexPathForCell:cell];
  656. // }
  657. //
  658. // if([self.textFieldtmp.restorationIdentifier isEqualToString:@"edit_from"])
  659. // [[_adapter get_item:[indexPath row]] setValue:@"" forKey:@"from"];
  660. // else
  661. // [[_adapter get_item:[indexPath row]] setValue:@"" forKey:@"to"];
  662. //
  663. // }
  664. // else
  665. // {
  666. // UIDatePicker *datePicker = (UIDatePicker *)[actionSheet viewWithTag:101];
  667. // NSDate *date = [datePicker date];
  668. // NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  669. // //设定时间格式,这里可以设置成自己需要的格式
  670. // [dateFormatter setDateFormat:@"MM/dd/yyyy"];
  671. // //用[NSDate date]可以获取系统当前时间
  672. // self.textFieldtmp.text = [dateFormatter stringFromDate:date];
  673. //
  674. //
  675. //
  676. //
  677. // // DebugLog(@"textFieldShouldBeginEditing text=%@",textField.text);
  678. // UITableViewCell *cell = (UITableViewCell *)[[self.textFieldtmp superview] superview];
  679. // // DebugLog(@"reuseid:%@",cell.reuseIdentifier);
  680. // // // UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
  681. //
  682. // NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  683. // if(indexPath==nil)
  684. // {//for ios7
  685. // cell = (UITableViewCell *) self.textFieldtmp.superview.superview.superview;
  686. // indexPath = [self.table indexPathForCell:cell];
  687. // }
  688. // // NSMutableDictionary * field =[_adapter get_item:[indexPath row]];
  689. //
  690. // if([self.textFieldtmp.restorationIdentifier isEqualToString:@"edit_from"])
  691. // [[_adapter get_item:[indexPath row]] setValue:[dateFormatter stringFromDate:date] forKey:@"from"];
  692. // else
  693. // [[_adapter get_item:[indexPath row]] setValue:[dateFormatter stringFromDate:date] forKey:@"to"];
  694. // // DebugLog(@"textFieldShouldBeginEditing row=%ld type=%@",(long)[indexPath row], [field valueForKey:@"type"]);
  695. // // if(![[field valueForKey:@"type"] isEqualToString:@"time"])
  696. //
  697. //
  698. //
  699. // }
  700. // }
  701. // else
  702. // {
  703. // // self.boolBtntmp = sender;
  704. // TableCellBool *cell = (TableCellBool *)[[self.boolBtntmp superview] superview];
  705. //
  706. // NSIndexPath *indexPath = [self.table indexPathForCell:cell];
  707. // if(indexPath==nil)
  708. // {//for ios7
  709. // cell = (TableCellBool *) self.boolBtntmp.superview.superview.superview;
  710. // indexPath = [self.table indexPathForCell:cell];
  711. // }
  712. //
  713. // if(buttonIndex==actionSheet.cancelButtonIndex)
  714. // {
  715. // [self.boolBtntmp setTitle:@"No" forState:UIControlStateNormal];
  716. // [[_adapter get_item:[indexPath row]] setValue:@"false" forKey:@"value"];
  717. // }
  718. // else if(buttonIndex==actionSheet.destructiveButtonIndex)
  719. // {
  720. // [self.boolBtntmp setTitle:@"Not set" forState:UIControlStateNormal];
  721. // [[_adapter get_item:[indexPath row]] setValue:@"" forKey:@"value"];
  722. // }
  723. // else
  724. // {
  725. // [self.boolBtntmp setTitle:@"Yes" forState:UIControlStateNormal];
  726. //
  727. //// cell.anameLabel.text =@"aaa";
  728. // // [self.boolBtntmp.titleLabel setText:@"yes"];
  729. // // self.boolBtntmp.titleLabel.text = @"Yes";
  730. // [[_adapter get_item:[indexPath row]] setValue:@"true" forKey:@"value"];
  731. // }
  732. //
  733. // }
  734. // // TODO:
  735. //}
  736. //-(void)changedDate:(id)sender{
  737. //
  738. // //实例化一个NSDateFormatter对象
  739. // NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  740. // //设定时间格式,这里可以设置成自己需要的格式
  741. // [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
  742. // //用[NSDate date]可以获取系统当前时间
  743. // self.textFieldtmp.text = [dateFormatter stringFromDate:self.datePicker.date];
  744. //
  745. //// [dateFormatter release];
  746. //
  747. //}
  748. @end