输入一个 Autocomplete TextField 会弹出另一个 Autocomplete TextField TableView
Posted
技术标签:
【中文标题】输入一个 Autocomplete TextField 会弹出另一个 Autocomplete TextField TableView【英文标题】:Inputing one Autocomplete TextField will pop up another Autocomplete TextField TableView 【发布时间】:2018-04-02 03:26:27 【问题描述】:我有 2 个自动完成功能 UITextField
在输入文本时会触发表格视图。其中一个我正在使用MVPlaceSearchTextField。我使用this 自定义构建的另一个。
当我输入 txtPlaceSearch 文本字段时,将触发 txtActivity UITextField
表视图。我怀疑当我输入 txtPlaceSearch 文本字段时会调用以下内容。
- (BOOL)textField:(UITextField *)txtActivity shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
但是当我输入txtActivityUITextField
时,txtPlaceSearch tableView不会触发。
当密钥位于 txtPlaceSearch 时,如何停止调用 txtActivity tableView?
#import "Search.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"
#import <GoogleMaps/GoogleMaps.h>
#import <GooglePlaces/GooglePlaces.h>
@interface Search()
@end
@implementation Search
AppDelegate *appDelegate;
NSString *sURL, *strResult, *sRemaining;
@synthesize lblStart;
@synthesize lblEnd;
@synthesize sCategory;
@synthesize autocompleteTableView;
@synthesize autocompleteMArray;
- (void)viewDidLoad
[super viewDidLoad];
_txtPlaceSearch.placeSearchDelegate = self;
_txtPlaceSearch.strApiKey = @"AIzaSyBJvZbCA-BiAE3HBgdrm6TTjAiVkYTU9Kk";
_txtPlaceSearch.superViewOfList = self.view; // View, on which Autocompletion list should be appeared.
_txtPlaceSearch.autoCompleteShouldHideOnSelection = YES;
_txtPlaceSearch.maximumNumberOfAutoCompleteRows = 5;
strResult = @"5|ALABAMA|Bohemian|CANADA|Denmark|Fin|";
NSString *sSeparator= @"|";
NSRange range = [strResult rangeOfString:sSeparator];
NSInteger position = range.location + range.length;
NSString *sLoop = [strResult substringToIndex:position-1];
sRemaining = [strResult substringFromIndex:position];
sCategory = [[NSMutableArray alloc] init];
for (int i = 0; i< [sLoop intValue]; i++)
range = [sRemaining rangeOfString:sSeparator];
position = range.location + range.length;
NSString *sCatName = [sRemaining substringToIndex:position-1];
sRemaining = [sRemaining substringFromIndex:position];
[sCategory addObject:sCatName];
NSLog(@"The Array : %@ ", sCategory);
//--- Configure the TableView
self.txtActivity.delegate = self;
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
//Optional Properties
_txtPlaceSearch.autoCompleteRegularFontName = @"HelveticaNeue-Bold";
_txtPlaceSearch.autoCompleteBoldFontName = @"HelveticaNeue";
_txtPlaceSearch.autoCompleteTableCornerRadius=0.0;
_txtPlaceSearch.autoCompleteRowHeight=35;
_txtPlaceSearch.autoCompleteTableCellTextColor=[UIColor colorWithWhite:0.131 alpha:1.000];
_txtPlaceSearch.autoCompleteFontSize=14;
_txtPlaceSearch.autoCompleteTableBorderWidth=1.0;
_txtPlaceSearch.showTextFieldDropShadowWhenAutoCompleteTableIsOpen=YES;
_txtPlaceSearch.autoCompleteShouldHideOnSelection=YES;
_txtPlaceSearch.autoCompleteShouldHideClosingKeyboard=YES;
_txtPlaceSearch.autoCompleteShouldSelectOnExactMatchAutomatically = YES;
_txtPlaceSearch.autoCompleteTableFrame = CGRectMake((self.view.frame.size.width-_txtPlaceSearch.frame.size.width)*0.5, _txtPlaceSearch.frame.size.height+100.0, _txtPlaceSearch.frame.size.width, 200.0);
[self cofigureAutoComTableView];
- (void)viewDidDisappear:(BOOL)animated
[super viewDidDisappear:animated];
-(BOOL)textFieldShouldReturn:(UITextField *)textField
[textField resignFirstResponder];
return YES;
- (BOOL)textField:(UITextField *)txtActivity shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:txtActivity.text];
substring = [substring
stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
autocompleteMArray = [[NSMutableArray alloc] init];
[autocompleteMArray removeAllObjects];
if ([substring length] > 0)
for(NSString *curString in sCategory)
//**** sCategory is a mutable array generated in another method not shown ****
NSRange substringRange = [curString rangeOfString:substring];
if (substringRange.length > 0)
[autocompleteMArray addObject:curString];
else
autocompleteTableView.hidden = YES;
NSLog(@"*******The autocompleteMArray : %@ ", autocompleteMArray);
[autocompleteTableView reloadData];
-(void)cofigureAutoComTableView
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.txtActivity.frame.origin.x,self.txtActivity.frame.origin.y+32,_txtActivity.frame.size.width, 200) style:UITableViewStylePlain];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
//autocompleteTableView.
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];
CALayer *layer = autocompleteTableView.layer;
[layer setMasksToBounds:YES];
[layer setCornerRadius: 0.0];
[layer setBorderWidth:1.0];
[layer setBorderColor:[[UIColor blackColor] CGColor]];
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
UIFont *myFont = [ UIFont fontWithName: @"HelveticaNeue" size: 14.0 ];
cell.textLabel.font = myFont;
if(indexPath.row % 2 == 0)
cell.backgroundColor = [UIColor lightGrayColor];
else
cell.backgroundColor = [UIColor whiteColor];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 35.0;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return autocompleteMArray.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [self.autocompleteTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.text = [autocompleteMArray objectAtIndex:indexPath.row];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
NSLog(@"Selected Cell %@", [autocompleteMArray objectAtIndex:indexPath.row]);
autocompleteTableView.hidden = YES;
_txtActivity.text = [autocompleteMArray objectAtIndex:indexPath.row];
#pragma mark - Place search Textfield Delegates
-(void)placeSearch:(MVPlaceSearchTextField*)textField ResponseForSelectedPlace:(GMSPlace*)responseDict
[self.view endEditing:YES];
NSLog(@"SELECTED ADDRESS :%@",responseDict);
-(void)placeSearchWillShowResult:(MVPlaceSearchTextField*)textField
-(void)placeSearchWillHideResult:(MVPlaceSearchTextField*)textField
-(void)placeSearch:(MVPlaceSearchTextField*)textField ResultCell:(UITableViewCell*)cell withPlaceObject:(PlaceObject*)placeObject atIndex:(NSInteger)index
if(index%2==0)
cell.contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
else
cell.contentView.backgroundColor = [UIColor whiteColor];
【问题讨论】:
【参考方案1】:在shouldChangeCharactersInRange
中,您应该检查UITextField
正在使用什么。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
if ([textField isEqual:txtActivity])
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:txtActivity.text];
substring = [substring
stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
else if ([textField isEqual:txtPlaceSearch])
// Do whatever you want when using |txtPlaceSearch|
return YES;
【讨论】:
以上是关于输入一个 Autocomplete TextField 会弹出另一个 Autocomplete TextField TableView的主要内容,如果未能解决你的问题,请参考以下文章
输入输入时如何限制对我的后端的 md-autocomplete 调用?
Elasticsearch:创建一个 autocomplete 输入系统 - 前端 + 后端
通过 rails3-jquery-autocomplete 使用多个输入字段