gpt4 book ai didi

ios - 输入一个 Autocomplete TextField 会弹出另一个 Autocomplete TextField TableView

转载 作者:行者123 更新时间:2023-12-01 19:44:51 33 4
gpt4 key购买 nike

我有 2 个自动完成 UITextField这将在输入文本时触发表格 View 。其中一个我正在使用MVPlaceSearchTextField .我使用 this 自定义构建的另一个.

当我输入 txtPlaceSearch 文本字段时,txtActivity UITextField表格 View 将触发。我怀疑当我输入 txtPlaceSearch 文本字段时会调用以下内容。

- (BOOL)textField:(UITextField *)txtActivity shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

但是当我输入 txtActivity UITextField , txtPlaceSearch tableView 不会触发。

当 key 位于 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];
}
}

Pic

最佳答案

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;
}

关于ios - 输入一个 Autocomplete TextField 会弹出另一个 Autocomplete TextField TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49604927/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com