CommonEditorAutoCompleteView.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.text = nil;
  21. [self.searchInput endEditing:true];
  22. self.arr_result = nil;
  23. self.hidden=true;
  24. [self.ResultTableView reloadData];
  25. }
  26. #pragma mark - TextField 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. - (void)textFieldDidEndEditing:(UITextField *)textField
  45. {
  46. if(self.returnValue)
  47. self.returnValue(textField.text);
  48. textField.text = nil;
  49. }
  50. //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
  51. //- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
  52. //- (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
  53. //- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
  54. //- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
  55. //
  56. //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
  57. //
  58. //- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
  59. //- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
  60. -(void) searchAutocomplete:(NSString*)keyword
  61. {
  62. if(keyword.length==0)
  63. {
  64. self.arr_result = nil;
  65. [self.ResultTableView reloadData];
  66. return;
  67. }
  68. __weak typeof(self) weakself = self;
  69. if(self.dataSource)
  70. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  71. NSString* searchid=[[NSUUID new] UUIDString];
  72. weakself.lastSearchID=searchid;
  73. NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword ];
  74. // NSArray* arr_ret=[weakself.dataSource sync_loadCadidate:keyword search_id:weakself.lastSearchID];
  75. // self.arr_result = [self.dataSource sync_loadCadidate:keyword search_id:[[NSUUID new] UUIDString]];
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. if([searchid isEqualToString:weakself.lastSearchID])
  78. {
  79. self.arr_result = arr_ret;
  80. [self.ResultTableView reloadData];
  81. }
  82. });
  83. // }
  84. });
  85. }
  86. #pragma mark - TableView Delegate & DataSource
  87. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  88. return self.arr_result.count;
  89. // return self.modeArray.count;
  90. }
  91. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  92. // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mode_cell" forIndexPath:indexPath];
  93. // // RAModel *model = self.modelist[indexPath.row];//[self.modeArray objectAtIndex:indexPath.row];
  94. //
  95. //// [cell setModeinfo:[self.modelist[indexPath.row] mutableCopy]];
  96. // // [cell setModel:model];
  97. // return cell;
  98. static NSString *MyIdentifier = @"MyIdentifier";
  99. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  100. if (cell == nil)
  101. {
  102. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
  103. reuseIdentifier:MyIdentifier];
  104. }
  105. cell.textLabel.text = self.arr_result[indexPath.row][@"value"];
  106. cell.detailTextLabel.text = self.arr_result[indexPath.row][@"description"];
  107. return cell;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  110. return 40.f;
  111. }
  112. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  113. // if(self.returnValue)
  114. // self.returnValue(self.arr_result[indexPath.row][@"value"]);
  115. self.searchInput.text = self.arr_result[indexPath.row][@"value"];
  116. [self.searchInput endEditing:true];
  117. // self.searchInput.text = nil;
  118. self.arr_result = nil;
  119. self.hidden=true;
  120. [self.ResultTableView reloadData];
  121. // RAModeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  122. // // RAModel *model = cell.model;
  123. // NSMutableDictionary* modeinfo= cell.modeinfo;
  124. //
  125. // if ([modeinfo[@"type"] isEqualToString:@"predef_query"]) {
  126. // [self processPredefQueryModel:modeinfo];
  127. //
  128. // } else if ([modeinfo[@"type"] isEqualToString:@"query"]) {
  129. // [self processQueryModel:modeinfo];
  130. //
  131. // } else if ([modeinfo[@"type"] isEqualToString:@"local_func"]) {
  132. // [self processLocalFunModel:modeinfo];
  133. //
  134. // } else if ([modeinfo[@"type"] isEqualToString:@"submode"]) {
  135. // [self processSubmodeModel:modeinfo];
  136. // }
  137. }
  138. @end