Ios 在UITextField值更改时立即填充UITableview数据

Ios 在UITextField值更改时立即填充UITableview数据,ios,Ios,我是IOS新手 我想实现类似于Facebook中google places autocomplete的功能。每按一次键,都会立即填充相应的数据。我编写了一个函数,其中数据按照UITextField中的值填充。但一旦用户单击“Return”键,就会发生这种情况。(正如我在“textfielddidediting”委托方法上调用的函数)。因此,我需要帮助知道在哪里(哪个委托方法)调用我的函数,以便根据UITextField中的文本更改立即填充适当的UITableView数据 - (void)MyFu

我是IOS新手

我想实现类似于Facebook中google places autocomplete的功能。每按一次键,都会立即填充相应的数据。我编写了一个函数,其中数据按照UITextField中的值填充。但一旦用户单击“Return”键,就会发生这种情况。(正如我在“textfielddidediting”委托方法上调用的函数)。因此,我需要帮助知道在哪里(哪个委托方法)调用我的函数,以便根据UITextField中的文本更改立即填充适当的UITableView数据

- (void)MyFunctionToGetData
{
    MyJSONURL = [NSString stringWithFormat:@"www.myurl.com?input=%@",txtMyText.text];
    dispatch_async(GDQueue, ^{Mydata = [NSMutableData dataWithContentsOfURL:[NSURL URLWithString: MyJSONURL]];
[self performSelectorOnMainThread:@selector(fetchedData:) withObject: Mydata waitUntilDone:YES]; });
}

- (void)MyFunctionTofetchedData:(NSMutableData *)responseData
{
NSError* error;    
MyArray = [NSJSONSerialization JSONObjectWithData: Mydata options:kNilOptions error:&error];
[tblMyTable reloadData];
}

//TableView delegate methods start
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [MyArray count];
}

-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"Cell";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];   
 if (cell==nil)
{    
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString * strDataText = [[MyArray objectAtIndex:indexPath.row] valueForKey:@“myKey"];           cell.textLabel.text = strDataText;   
return cell;

  }

  // I am calling my function here. By doing this i can get results once i edit the text and then click “Return” key. Whereas i want the different result on each key press action on that textfield.

  - (void)textFieldDidEndEditing:(UITextField *)textField

  {
   [self MyFunctionToGetData];


  }
谢谢

[textField addTarget:self action:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged]; 
在方法上,

-(void)textFieldDidChange :(UITextField *)theTextField{
NSLog( @"text changed: %@", theTextField.text);
}

您可以在文本字段中看到当前文本。每次用户键入一个新字符,并且您填充tableView时

确定,这样,您应该使用以下TextField委托方法:

textField:shouldChangeCharactersInRange:replacementString:



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"%@",textField.text);
return YES;

},每次按下键盘按钮时都会调用此方法。希望对您有所帮助

请查找此处添加的示例代码。我就是这样做的。谢谢submitRequest方法正在做什么?抱歉,它应该是“MyFunctionToGetData”而不是“submitRequest”。您的方法应该是myFunctionToGetDataContainingString:(NSString*)string;基于该字符串,您将只获得以该字符串开头的数据,因此您可以使用所需的数据填充tableView