Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 输入一个自动完成文本字段将弹出另一个自动完成文本字段表视图_Ios_Objective C_Uitableview_Autocomplete_Uitextfield - Fatal编程技术网

Ios 输入一个自动完成文本字段将弹出另一个自动完成文本字段表视图

Ios 输入一个自动完成文本字段将弹出另一个自动完成文本字段表视图,ios,objective-c,uitableview,autocomplete,uitextfield,Ios,Objective C,Uitableview,Autocomplete,Uitextfield,我有两个自动完成的UITextField,当输入文本时会触发一个表视图。其中一个我正在使用。另一个我自定义构建使用 当我输入txtPlaceSearch文本字段时,将触发TXTPactivity UITextField表视图。我怀疑在我输入txtPlaceSearch文本字段时调用了以下命令 - (BOOL)textField:(UITextField *)txtActivity shouldChangeCharactersInRange:(NSRange)range replacementSt

我有两个自动完成的UITextField,当输入文本时会触发一个表视图。其中一个我正在使用。另一个我自定义构建使用

当我输入txtPlaceSearch文本字段时,将触发TXTPactivity 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];
    }
}

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