开发者

Help with Search bar scope buttons

开发者 https://www.devze.com 2023-02-20 21:04 出处:网络
I have a UI that displays data from a user table like FirstName, LastName, Email,etc. Now i want to create a search bar along with scope buttons that filters data depending on the scope button clicked

I have a UI that displays data from a user table like FirstName, LastName, Email,etc. Now i want to create a search bar along with scope buttons that filters data depending on the scope button clicked. I have 2 scope buttons, FirstName and LastName. By default FirstName button is selected. Below is how I add my data to a mutablearray,

userData = [[NSMutableArray alloc] init];


    for (NSDictionary *tmpDic in response) {         
        [userData addObject: [NSString stringWithFormat: @"%@ %@", 
                                [tmpDic valueForKey: @"FirstName"],[tmpDic valueForKey: @"LastName"]]]; 
    }

My search code,

- (void) searchTableView {

    NSString *searchText = theSearchBar.text;
    NSMutableArray *searchArray = [[NSMutableArray alloc] init];

    for (NSString *sTemp in userData)
    {
        NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

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

    NSLog(@"Copied data is:%@", copyuserData);

    [searchArray release];
    searchArray = nil;
}

The above code works well for searching the userData array, b开发者_运维技巧ut i am not sure how will i change the code so that depending on FirstName, LastName scope buttons it will display the result. how i will hook up the buttons to the search bar so that it only display result depending on what scope bar button is clicked. Thanks in advance..


You need to do two things: 1. Look at the value of the searchBar.selectedScopeButtonIndex - this will tell you if you need to search first names or last names. 2. Depending on the scope button, you want to search either the first part of each array item or the second part. There are lots of ways to do this. Probably the easiest is to keep 2 parallel arrays, firstNames and lastNames, which you populate from tmpDic. Then for the actual search, you could either loop through firstNames or lastNames, or do a for(int j=0;j<[firstNames count]; j++) and get [firstNames objectAtIndex:j] and compare that to your temp string. If any string matches, add it to your results array.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号