我有2个自动完成UITextField
,它会在输入文本时触发表格视图。其中一个我正在使用MVPlaceSearchTextField。我使用this自定义构建的另一个。
当我输入到txtPlaceSearch textField时,将触发txtActivity UITextField
表视图。我怀疑当我输入到txtPlaceSearch文本字段时被调用。
- (BOOL)textField:(UITextField *)txtActivity shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
但是当我输入txtActivity UITextField
时,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];
}
}
答案 0 :(得分:2)
在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;
}