简体   繁体   English

如何实现UITableView搜索功能

[英]How to implement UITableView search functionality

My iPhone application has a UITable View implemented with search functionality in it. 我的iPhone应用程序具有一个实现了搜索功能的UITable视图。 The values in the table are grouped into sections from AZ. 表中的值分为AZ部分。 Whenever a user tap on particular cell in the table it loads a detail view controller which gives all the values of that particular user. 每当用户点击表中的特定单元格时,它就会加载一个详细视图控制器,该控制器会提供该特定用户的所有值。 Now my problem is whenever I search some contact and tap on a particular user to check his detail view it always returns a contact starting with letter A. So, my doubt is how to implement this search functionality. 现在我的问题是,每当我搜索某些联系人并点击特定用户以查看其详细信息视图时,它总是返回以字母A开头的联系人。因此,我的疑问是如何实现此搜索功能。 Is there a way to get the name of the contact I tapped..Please check this screenshot.. For example if I search some contact starting with letter 'B' and tap on that contact it loads the detail view of a contact starting with letter 'A'. 有没有一种方法可以获取我点击的联系人的姓名。请检查此屏幕截图。例如,如果我搜索一些以字母“ B”开头的联系人并点击该联系人,则会加载以字母开头的联系人的详细视图'一种'。 I get all the values from the database. 我从数据库中获取所有值。 Can you please help me out... 你能帮我一下吗... 替代文字

This is the code: 这是代码:

The code I wrote here is in a method: 我在这里编写的代码在一个方法中:

I am getting all the contacts from database and assigning to an array contacts. 我从数据库中获取所有联系人,并将其分配给数组联系人。 Then I am grouping all the contacts according to the alphabets and grouping everything into a dictionary with keys as AZ and values as name of contacts starting with these letters. 然后,我根据字母将所有联系人分组,并将所有内容分组到一个字典中,键为AZ,值作为以这些字母开头的联系人的名称。 Now when I search for a particular contact his name may start with either A ,B or Z..so in the search bar when I search for a particular contact for example a contact starting with letter Z, in this case it gives the details of a person with A. I want this to change so that whenever I tap on a particular contact it should load its details. 现在,当我搜索特定联系人时,当我搜索特定联系人(例如,以字母Z开头的联系人)时,其名称可能在搜索栏中以A,B或Z..so开头。我希望对此进行更改,以便每当我点击某个特定联系人时都应加载其详细信息。 I am unable to figure out how to do it.. 我不知道该怎么做。

    contacts = [[db getContacts:@"Contacts"] componentsSeparatedByString:@","];
    [db cleanup];

    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    NSString *charString;
    for (int i=65; i<91; i++) {
        charString = [NSString stringWithFormat:@"%c",(char *)i];
        [tempArray addObject:charString];
    }
    [charString release];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];


    for (int i=0; i<[tempArray count]; i++) {
        NSMutableArray *contactsByIndex = [[[NSMutableArray alloc] init]autorelease];
        NSString *tempChar = [tempArray objectAtIndex:i];

        for (int j=0; j<[contacts count]-1; j++)
        {

            NSString *test = [contacts objectAtIndex:j];
            NSString *tempString = [test substringToIndex:1];

            if ([tempString isEqualToString:tempChar]) {
                [contactsByIndex addObject:[contacts objectAtIndex:j]];
            }


        }
        [dict setObject:contactsByIndex forKey:[tempArray objectAtIndex:i]];
    }
        self.contactNames = dict;

    NSArray *array = [[contactNames allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

    self.contactKeys = array;

    [dict release];
    [tempArray release];

    //---display the searchbar--- 
    self.tableView.tableHeaderView = searchBar; 
    searchBar.autocorrectionType = UITextAutocorrectionTypeYes;

    listOfContacts = [[NSMutableArray alloc] init]; 
    for (NSString *key in array)  
    {

        NSArray *contactsArray = [contactNames objectForKey:key]; 
        for (NSString *name in contactsArray) {
            [listOfContacts addObject:name];
        }
    }


   - (void) searchContactsTableView {

    //---clears the search result--- 
    [searchResult removeAllObjects];
    for (NSString *str in listOfContacts) {
        NSRange titleResultsRange = [str rangeOfString:searchBar.text options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0) 
            [searchResult addObject:str];

    }

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    NSString *selectedRow=nil;
    if (isSearchOn) {
        selectedRow=[searchResult objectAtIndex:indexPath.row];
    }

    DetailViewController *detailViewController;
    int section_index=[indexPath indexAtPosition:[indexPath length]-2];
    int sugarid_Index = [indexPath indexAtPosition: [indexPath length]-1];
    NSString* sectionName=[contactKeys objectAtIndex:section_index];
    NSLog(@"%@",sectionName);

//This is a method which gets the details of a particular contact based on the section and the row selected..Contacts is the table name

    NSString *getContact=[db getId:@"Contacts" bySection:sectionName andIndex:sugarid_Index];
    id=[db getContact:@"Contacts" id:getContact];
    // Pass the selected object to the new view controller.
    detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    detailViewController.eachContact=contactForSugarId;
    [self.navigationController pushViewController:detailViewController animated:YES];

}

When I search for a contact it should search for the name in database and should return its details. 当我搜索联系人时,它应该在数据库中搜索姓名并返回其详细信息。 Is there a way to do it..please let me know or is there a way to get the name of the cell ie the contact name so that I can use that in one of my database methods to retrieve the details of the contact I selected. 有没有办法..请让我知道,或者有办法获取单元格的名称,即联系人姓名,以便我可以在一种数据库方法中使用该名称来检索我选择的联系人的详细信息。

Off hand it sounds like you're looking in the wrong array of contacts after the search. 听起来好像您在搜索之后寻找的联系人数组有误。 You need to have two arrays. 您需要有两个数组。 One with all the contacts, and one with the filtered contacts. 一种是所有联系人,另一种是经过筛选的联系人。 When you search, put all the results in order in the filtered list, and pull the details from that one. 搜索时,将所有结果按顺序放在过滤列表中,然后从该列表中提取详细信息。

Does this make sense? 这有意义吗?

If not, try posting a bit of code, and explaining your structure. 如果没有,请尝试发布一些代码,并解释您的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM