如何在特定数量的文本长度上为多个文本字段实现自动完成

时间:2016-07-13 04:42:17

标签: ios objective-c uitableview autocomplete

这种情况下有很多库可用,但我的情况有点不同。我有一个ViewController有三个文本字段。我必须为每个文本字段实现自动完成,并在文本字段下方显示建议。对于textfield的所有数据源,我依赖于web服务。问题是它只适用于一个文本字段。请建议一些有效的方法来做到这一点。 1)输入两个字符后,建议应显示在文本字段的下方或上方。 UI不应该被阻止,用户可以继续输入。(我在服务器上调用后台线程上有两个字符,并接收响应显示为建议。过滤器逻辑在服务器上实现)
2)如果用户没有从建议中选择任何内容,则文本字段数据将被发送到服务器 3)屏幕上任意点击都应该隐藏建议。(问题是,如果我使用点击手势识别器,tableview单元格也没有触摸)

创建表格视图以显示建议

- (void)viewDidLoad {
[super viewDidLoad];    
self.orderNoTextField.delegate = self;
self.empNameTextField.delegate = self;
self.custNameTextField.delegate = self;

rectForEmp = CGRectMake(0, self.empNameTextField.frame.origin.y + self.empNameTextField.frame.size.height, self.empNameTextField.frame.size.width, 120);

rectForCust = CGRectMake(0, self.orderNoTextField.frame.origin.y, self.custNameTextField.frame.size.width, 120);

autocompleteTableView = [[UITableView alloc] initWithFrame:rectForCust style:UITableViewStylePlain];
autocompleteTableView.backgroundColor = [UIColor clearColor];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
autocompleteTableView.canCancelContentTouches = NO;


autocompEmpTableView = [[UITableView alloc] initWithFrame:rectForEmp style:UITableViewStylePlain];
autocompEmpTableView.backgroundColor = [UIColor clearColor];
autocompEmpTableView.delegate = self;
autocompEmpTableView.dataSource = self;
autocompEmpTableView.scrollEnabled = YES;
autocompEmpTableView.hidden = YES;
autocompEmpTableView.canCancelContentTouches = NO; 
[self.view addSubview:autocompleteTableView];
[self.view addSubview:autocompEmpTableView];
}

获取建议

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

if (self.custNameTextField.text.length == 2)
{
    autocompEmpTableView.hidden = YES;

    [self generateDataForCustomerName];

}
else if (self.empNameTextField.text.length == 2)
{
    autocompleteTableView.hidden = YES;

    [self generateDataForEmployeeName];

}
   return YES;
}
- (void)generateDataForCustomerName
{

/** get background thread for datasource of autocomplete **/


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    {
        httpClient = [[KKHttpClient alloc]init];
        //httpClient.responseDelegate = self;
        //NSString *params = [self.empNameTextField text];

        params = [NSString stringWithFormat:@"{\"rqBody\":{\"searchKey\":\"%@\"}}",[self.custNameTextField text]];

        relativeUrl = [NSString stringWithFormat:@"auth/getcustomernamelist"];
        [httpClient connectWithUrl:relativeUrl withData:params completion:^(NSMutableDictionary *responseJson)
         {
             NSLog(@"response check for new methods:%@", responseJson);

             dispatch_async(dispatch_get_main_queue(), ^{

                 //Here returns to main thread to update the UI.

                 /** initialize the array and add object to it**/

                 custNameData = [[NSMutableArray alloc]init];
                 custNameData = [responseJson[@"rsBody"]valueForKey:@"msg"];

                 autocompleteTableView.hidden = NO;
                 [autocompleteTableView reloadData];

             });


         }];

    }

});
}

Tableview显示建议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
if(tableView == autocompleteTableView)
{
    return empNameData.count;
}
else
{
    return custNameData.count;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

if (tableView == autocompEmpTableView) {
    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompEmpRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];

    }
    employeeName = [[empNameData valueForKey:@"employeeName"]objectAtIndex:indexPath.row];

    employeeId = [[empNameData valueForKey:@"employeeId"]objectAtIndex:indexPath.row];

    NSString *autoCompleteText = [NSString stringWithFormat:@"%@ %@%@%@", employeeName, @"(", employeeId, @")"];

    cell.textLabel.text = autoCompleteText;

    return cell;

}
else
{

UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];

if (cell == nil) {

    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier];

}


    customerName = [[custNameData valueForKey:@"customerName"]objectAtIndex:indexPath.row];

    customerCode = [[custNameData valueForKey:@"customerCode"]objectAtIndex:indexPath.row];

    NSString *autoCompleteText = [NSString stringWithFormat:@"%@ %@ %@%@", customerName, @"(", customerCode, @")"];

    cell.textLabel.text = autoCompleteText;

return cell;
}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *title = selectedCell.textLabel.text;

if(tableView == autocompEmpTableView)
{
    self.empNameTextField.text = title;
    autocompEmpTableView.hidden = YES;
}
else{
    self.custNameTextField.text = title;
    autocompleteTableView.hidden =YES;
}
    }

0 个答案:

没有答案