0

我有一个文本字段,用户在其中输入姓名。现在,当在文本字段中输入字母时,让我们说“T”,那么我如何从 iphone 的联系人列表中获得关于输入的字母的姓名建议。

我还需要显示与输入的名称相对应的适当数字。

我已经浏览了苹果文档

任何人都可以帮助我提出宝贵的建议或示例代码片段:

提前致谢 :)

编辑

我能够在日志(控制台)中查看联系人,因为我在 viewDidLoad 方法中编写了 Anil Kothari 先生建议的代码

正如Anil Kothari 先生所建议的,我已经为文本字段委托方法实现了搜索条码,如下所示:

- (void)textFieldDidBeginEditing:(UITextField *)atextField
{

    if(searching)
        return;
    searching = YES;    
}

- (void) searchTableView
{

    textField = [self.fields objectAtIndex:0];
    NSString *searchText = textField.text;

    for (UIView *subview in searchBar.subviews)
    {
        if ([subview isKindOfClass:[UITextField class]])
        {
            textField = (UITextField *)subview;
            break;
        }
    }
    textField.enablesReturnKeyAutomatically = NO;

    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    //searchArray contains matched names of friends with the searching string

    for (NSString *sTemp in contactList)
    {

        txtToSearch =[[NSString alloc] initWithString:[sTemp substringWithRange:NSMakeRange(0,[searchText length])]];

        textField = [self.fields objectAtIndex:0];

        txtToSearch = textField.text;

        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];

    }
    [searchArray release];
    searchArray = nil;
}

-(BOOL)textField:(UITextField *)atextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
[copyListOfItems removeAllObjects];

    if([string length] > 0) 
    {
        searching = YES;
        [self searchTableView];
    }
    else 
    {
        searching = NO;
    }
}

但仍然无法正常工作:(

4

2 回答 2

1

检查我已经在某些应用程序中完成的代码:-

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tblSearchList.tableHeaderView = searchBar;
    contactList=[[NSMutableArray alloc]init];
    copyListOfItems=[[NSMutableArray alloc]init];

    ABAddressBookRef addressBook = ABAddressBookCreate( );
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

    for ( int i = 0; i < nPeople; i++ )
    {
        ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
        NSString *contactName =[[NSString alloc] initWithString:(NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty)]; 
        NSLog(@"% @ ",contactName);
        [contactList addObject:contactName];
        [contactName release];
    }
}

searchBar 代表的

- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
    if(searching)
        return;
    searching = YES;    
}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

    [copyListOfItems removeAllObjects];

    if([searchText length] > 0) {
        searching = YES;
        [self searchTableView];
    }
    else {
        searching = NO;
    }
    [self.tblSearchList reloadData];
}

- (void) searchTableView {

    NSString *searchText = searchBar.text;

    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    //searchArray contains matched names of friends with the searching string

    for (NSString *sTemp in contactList)
    {

        NSString *txtToSearch =[[NSString alloc] initWithString:[sTemp substringWithRange:NSMakeRange(0,[searchText length])]];

        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (titleResultsRange.length > 0)
            [copyListOfItems addObject:sTemp];

    }
    [searchArray release];
    searchArray = nil;
}

- (void) doneSearching_Clicked:(id)sender {
    searchBar.text = @"";
    [searchBar resignFirstResponder];
    searching = NO;
    [self.tblSearchList reloadData];
}

tableView 代表的

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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (searching)
        return [copyListOfItems count];

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if(searching)
        return @"Search Results";
    return @"";
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    if(searching) 
        cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];

    return cell;
}

随意询问任何进一步的问题..

于 2012-01-12T09:32:32.580 回答
0

你应该使用 UISearchBar,实现委托方法:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

从 searchText 中获取字母 \s 并使用 ABAddressBookRef 获取联系人,然后相应地更新 SearchDisplayController..

希望这有所帮助

于 2012-01-12T08:49:05.563 回答