CommonEditorAutoCompleteView.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // CommonEditorAutoCompleteView.m
  3. // Storage Master
  4. //
  5. // Created by Ray on 12/12/2017.
  6. // Copyright © 2017 R&J. All rights reserved.
  7. //
  8. #import "CommonEditorAutoCompleteView.h"
  9. @implementation CommonEditorAutoCompleteView
  10. /*
  11. // Only override drawRect: if you perform custom drawing.
  12. // An empty implementation adversely affects performance during animation.
  13. - (void)drawRect:(CGRect)rect {
  14. // Drawing code
  15. }
  16. */
  17. - (IBAction)onCancelClick:(id)sender {
  18. // [self.active_field endEditing:true];
  19. // self.hidden = true;
  20. [self.searchInput endEditing:true];
  21. self.searchInput.text = nil;
  22. self.arr_result = nil;
  23. self.hidden=true;
  24. [self.ResultTableView reloadData];
  25. }
  26. #pragma mark - SearchBar Delegate
  27. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  28. // autocompleteTableView.hidden = NO;
  29. NSString *substring = [NSString stringWithString:textField.text];
  30. substring = [substring stringByReplacingCharactersInRange:range withString:string];
  31. // [self searchAutocompleteEntriesWithSubstring:substring];
  32. // self.active_field textrangef
  33. bool canchange=[self.active_field.delegate textField:self.active_field shouldChangeCharactersInRange:range replacementString:string];
  34. // bool canchange=[self.active_field shouldChangeTextInRange:nil replacementText:string];
  35. if(canchange)
  36. {
  37. // [self searchAutocomplete:substring];
  38. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword];
  39. self.keyword = substring;
  40. [self performSelector:@selector(searchAutocomplete:) withObject:self.keyword afterDelay:0.8];
  41. }
  42. return canchange;
  43. }
  44. //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
  45. //- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
  46. //- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
  47. //- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
  48. //- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
  49. //
  50. //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
  51. //
  52. //- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
  53. //- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
  54. -(void) searchAutocomplete:(NSString*)keyword
  55. {
  56. if(keyword.length==0)
  57. {
  58. self.arr_result = nil;
  59. [self.ResultTableView reloadData];
  60. return;
  61. }
  62. if(self.dataSource)
  63. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  64. self.arr_result = [self.dataSource sync_loadCadidate:keyword];
  65. dispatch_async(dispatch_get_main_queue(), ^{
  66. [self.ResultTableView reloadData];
  67. });
  68. // }
  69. });
  70. }
  71. #pragma mark - TableView Delegate & DataSource
  72. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  73. return self.arr_result.count;
  74. // return self.modeArray.count;
  75. }
  76. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  77. // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mode_cell" forIndexPath:indexPath];
  78. // // RAModel *model = self.modelist[indexPath.row];//[self.modeArray objectAtIndex:indexPath.row];
  79. //
  80. //// [cell setModeinfo:[self.modelist[indexPath.row] mutableCopy]];
  81. // // [cell setModel:model];
  82. // return cell;
  83. static NSString *MyIdentifier = @"MyIdentifier";
  84. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  85. if (cell == nil)
  86. {
  87. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
  88. reuseIdentifier:MyIdentifier];
  89. }
  90. cell.textLabel.text = self.arr_result[indexPath.row][@"value"];
  91. cell.detailTextLabel.text = self.arr_result[indexPath.row][@"description"];
  92. return cell;
  93. }
  94. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  95. return 40.f;
  96. }
  97. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  98. if(self.returnValue)
  99. self.returnValue(self.arr_result[indexPath.row][@"value"]);
  100. [self.searchInput endEditing:true];
  101. self.searchInput.text = nil;
  102. self.arr_result = nil;
  103. self.hidden=true;
  104. [self.ResultTableView reloadData];
  105. // RAModeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  106. // // RAModel *model = cell.model;
  107. // NSMutableDictionary* modeinfo= cell.modeinfo;
  108. //
  109. // if ([modeinfo[@"type"] isEqualToString:@"predef_query"]) {
  110. // [self processPredefQueryModel:modeinfo];
  111. //
  112. // } else if ([modeinfo[@"type"] isEqualToString:@"query"]) {
  113. // [self processQueryModel:modeinfo];
  114. //
  115. // } else if ([modeinfo[@"type"] isEqualToString:@"local_func"]) {
  116. // [self processLocalFunModel:modeinfo];
  117. //
  118. // } else if ([modeinfo[@"type"] isEqualToString:@"submode"]) {
  119. // [self processSubmodeModel:modeinfo];
  120. // }
  121. }
  122. @end