// // CommonEditorAutoCompleteView.m // Storage Master // // Created by Ray on 12/12/2017. // Copyright © 2017 R&J. All rights reserved. // #import "CommonEditorAutoCompleteView.h" @implementation CommonEditorAutoCompleteView /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (IBAction)onCancelClick:(id)sender { // [self.active_field endEditing:true]; // self.hidden = true; [self.searchInput endEditing:true]; self.searchInput.text = nil; self.arr_result = nil; self.hidden=true; [self.ResultTableView reloadData]; } #pragma mark - SearchBar Delegate - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // autocompleteTableView.hidden = NO; NSString *substring = [NSString stringWithString:textField.text]; substring = [substring stringByReplacingCharactersInRange:range withString:string]; // [self searchAutocompleteEntriesWithSubstring:substring]; // self.active_field textrangef bool canchange=[self.active_field.delegate textField:self.active_field shouldChangeCharactersInRange:range replacementString:string]; // bool canchange=[self.active_field shouldChangeTextInRange:nil replacementText:string]; if(canchange) { // [self searchAutocomplete:substring]; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchAutocomplete:) object:self.keyword]; self.keyword = substring; [self performSelector:@selector(searchAutocomplete:) withObject:self.keyword afterDelay:0.8]; } return canchange; } //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing. //- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder //- (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 //- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called //- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing: // //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text // //- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications) //- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore. -(void) searchAutocomplete:(NSString*)keyword { if(keyword.length==0) { self.arr_result = nil; [self.ResultTableView reloadData]; return; } if(self.dataSource) dispatch_async(dispatch_get_global_queue(0, 0), ^{ self.arr_result = [self.dataSource sync_loadCadidate:keyword]; dispatch_async(dispatch_get_main_queue(), ^{ [self.ResultTableView reloadData]; }); // } }); } #pragma mark - TableView Delegate & DataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.arr_result.count; // return self.modeArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mode_cell" forIndexPath:indexPath]; // // RAModel *model = self.modelist[indexPath.row];//[self.modeArray objectAtIndex:indexPath.row]; // //// [cell setModeinfo:[self.modelist[indexPath.row] mutableCopy]]; // // [cell setModel:model]; // return cell; static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MyIdentifier]; } cell.textLabel.text = self.arr_result[indexPath.row][@"value"]; cell.detailTextLabel.text = self.arr_result[indexPath.row][@"description"]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 40.f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(self.returnValue) self.returnValue(self.arr_result[indexPath.row][@"value"]); [self.searchInput endEditing:true]; self.searchInput.text = nil; self.arr_result = nil; self.hidden=true; [self.ResultTableView reloadData]; // RAModeCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // // RAModel *model = cell.model; // NSMutableDictionary* modeinfo= cell.modeinfo; // // if ([modeinfo[@"type"] isEqualToString:@"predef_query"]) { // [self processPredefQueryModel:modeinfo]; // // } else if ([modeinfo[@"type"] isEqualToString:@"query"]) { // [self processQueryModel:modeinfo]; // // } else if ([modeinfo[@"type"] isEqualToString:@"local_func"]) { // [self processLocalFunModel:modeinfo]; // // } else if ([modeinfo[@"type"] isEqualToString:@"submode"]) { // [self processSubmodeModel:modeinfo]; // } } @end