自动完成对UITextField,用于iOS版 [英] AutoComplete for UITextfield in iOS

查看:133
本文介绍了自动完成对UITextField,用于iOS版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作新的iOS应用程序。在该应用程序,我有5个文本框,并在这些文本框是
1.先将利息2.第二利息高达5 intersts。

I'm working new iOS application. In that app, i have 5 textfields and in those textfields are 1. first interest 2. second interest upto 5 intersts.

我需要添加自动完成那些5文本框。我已搜查谷歌1天。我得到了一些论坛和教程。但是,我甚至试图与Github上链接也。

I need to add Autocomplete for those 5 textfields. I have searched google for 1 day. I got some forums and tutorial for that. But I even tried with Github links also.

根据我的要求是,我有一个数据数组是从我的服务器获取。在这阵我有像,咖啡,板球等,那是自动完成的数据。我需要显示时用户输入的文本字段中的文本,如果涉及到我的数据数组,数组,需要显示低于文本框的。

According to my requirement is, i have an array of data which is getting from my server. In that array i have data like, coffee, cricket, etc. That is Autocomplete data. I need to display that array whenever user entered text in textfield, if its related to my array of data, need to display below of that textfields.

为了这个目的,我用下面的code。

For that purpose i used following code.

** //字符串在搜索文本框

**// String in Search textfield

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

NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

**

//从搜索文本字段以字符串,并将其与自动完成阵列比较

// Take string from Search Textfield and compare it with autocomplete array

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

// Put anything that starts with this substring into the autoCompleteArray
// The items in this array is what will show up in the table view

[autoCompleteArray removeAllObjects];
NSLog(@"autoCompleteArray %@",autoCompleteArray);
for(NSString *curString in elementArray) {
    NSRange substringRangeLowerCase = [curString rangeOfString:[substring lowercaseString]];
    NSRange substringRangeUpperCase = [curString rangeOfString:[substring uppercaseString]];

    if (substringRangeLowerCase.length != 0 || substringRangeUpperCase.length != 0) {
        [autoCompleteArray addObject:curString];
    }
}

autoCompleteTableView.hidden = NO;
[autoCompleteTableView reloadData];
      }

和我在 viewDidLoad中方法创建的tableview为 AutocompleteTableview

And I created tableview as AutocompleteTableview in ViewDidLoad method.

**问题是,如果我输入文本假设为C,从我的任何显示文本包含在tableview中的C字母的数据阵列。但是,如果我输入COFF没有数据在显示的tableview。还如何验证在 tableviewdidselectrowatindexpath 委托方法,文本框用户点击。我试图与那些文本字段指定标签,但其在文本字段委托方法只是工作,而不是在其他地方。所以,每当我从选定的tableview的数据,第一只文本框获取数据而不是其他文本框。

**Issue is, if i type text as suppose "c", from my array of data displaying whatever text contains as "c" letter in tableview. But, if i typed "coff" no data displaying in that tableview. Also how to validate which textfield user clicking in tableviewdidselectrowatindexpath delegate method. I tried with assigning tag for those textfields, but its working in textfields delegate methods only,not in other place. so, whenever i selected data from tableview, first textfield only taking data not other textfield.

请给您的宝贵建议,这是显示 UITextfields iOS中多个文本框,以及如何处理的tableview用于显示数据的自动完成的最佳途径。如果我的内容任何错误原谅我,请提供您的宝贵建议,以解决此问题。谢谢****

Please give your valuable suggestion, which is the best way to display the autocomplete for UITextfields in iOS for multiple textfields and how to handle tableview for displaying data. If anything mistakes in my content forgive me and please provide your valuable suggestions to fix this issue. Thanks****

推荐答案

举两个全局数组

NSMutableArray *muary_Interest_Main;
NSMutableArray *muary_Interest_Sub;

在viewDidLoad方法

IN viewDidLoad Method

muary_Interest_Main = [[NSMutableArray alloc]initWithObjects:@"Cricket",@"Dancing",@"Painting",@"Swiming",@"guitar",@"movie",@"boxing",@"drum",@"hockey",@"chessing",@"gamming", 
    @"hunting",@"killing",@"shoping",@"jamm"@"zooming", nil]; 
muary_Interest_Sub = [[NSMutableArray alloc]init]; 


tbl_Search = [[UITableView alloc] initWithFrame: 
CGRectMake(4, 200, 320, 120) style:UITableViewStylePlain]; 
tbl_Search.delegate = self; 
tbl_Search.dataSource = self; 
tbl_Search.scrollEnabled = YES; 

[self.tbl_Search registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellIdentifier"]; 
[self.view addSubview:self.tbl_Search]; 


[tbl_Search setHidden:TRUE];

现在在文本框代表写了下面code。

Now write a below code in textfield delegates.

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
NSLog(@"%d",textField.tag); 
int_TextFieldTag = textField.tag; 

[self searchText:textField replacementString:@"Begin"]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
[textField resignFirstResponder]; 
tbl_Search.hidden = TRUE; 
return YES; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
tbl_Search.hidden = TRUE; 
} 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
[self searchText:textField replacementString:string]; 
return YES; 
}

写下搜索文本的方法

Write a method for search text

-(void) searchText:(UITextField *)textField replacementString:(NSString *)string
{

    if(int_TextFieldTag == 1)
    {
        tbl_Search.frame = CGRectMake(4, 200, 320, 120);

    }
    else if(int_TextFieldTag == 2)
    {
        tbl_Search.frame = CGRectMake(4, 248, 320, 120);
    }
    else if(int_TextFieldTag == 3)
    {
        tbl_Search.frame = CGRectMake(4, 268, 320, 120);
    }
    else if(int_TextFieldTag == 4)
    {
        tbl_Search.frame = CGRectMake(4, 268, 320, 120);
    }
    else
    {
        tbl_Search.frame = CGRectMake(4, 268, 320, 120);
    }



    NSString *str_Search_String=[NSString stringWithFormat:@"%@",textField.text];
    if([string isEqualToString:@"Begin"])
        str_Search_String=[NSString stringWithFormat:@"%@",textField.text];
    else if([string isEqualToString:@""])
        str_Search_String = [str_Search_String substringToIndex:[str_Search_String length] - 1];
    else
        str_Search_String=[str_Search_String stringByAppendingString:string];

    muary_Interest_Sub=[[NSMutableArray alloc] init];
    if(str_Search_String.length>0)
    {
        NSInteger counter = 0;
        for(NSString *name in muary_Interest_Main)
        {
            NSRange r = [name rangeOfString:str_Search_String options:NSCaseInsensitiveSearch];
            if(r.length>0)
            {
                [muary_Interest_Sub addObject:name];
            }

            counter++;

        }

        if (muary_Interest_Sub.count > 0)
        {
            NSLog(@"%@",muary_Interest_Sub);
            tbl_Search.hidden = FALSE;
            [self.tbl_Search reloadData];
        }
        else
        {
           tbl_Search.hidden = TRUE;
        }



    }
    else
    {
        [tbl_Search setHidden:TRUE];

    }

}

泰伯维代表法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; 
if (cell == nil) 
{ 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"]; 
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
; 
} 
cell.textLabel.text = [muary_Interest_Sub objectAtIndex:indexPath.row]; 
return cell; 

} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
[self.view endEditing:YES]; 
if(int_TextFieldTag == 1) 
{ 
txt1.text=[muary_Interest_Sub objectAtIndex:indexPath.row]; 

} 
else if(int_TextFieldTag == 2) 
{ 
txt2.text=[muary_Interest_Sub objectAtIndex:indexPath.row]; 
} 
else if(int_TextFieldTag == 3) 
{ 
txt3.text=[muary_Interest_Sub objectAtIndex:indexPath.row]; 
} 
else if(int_TextFieldTag == 4) 
{ 
txt4.text=[muary_Interest_Sub objectAtIndex:indexPath.row]; 
} 
else 
{ 
txt5.text=[muary_Interest_Sub objectAtIndex:indexPath.row]; 
} 

} 

这也适用于文本框的退格。
尝试这个。希望这将满足您的要求。

This also works on backspace of textfield. Try this. Hope this will suit your requirements.

这篇关于自动完成对UITextField,用于iOS版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆