帮助与搜索栏按钮范围 [英] Help with Search bar scope buttons

查看:123
本文介绍了帮助与搜索栏按钮范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像从名字,姓氏,电子邮件等用户表中显示数据的UI。现在我想创建一个范围按钮以及一个搜索栏的数据取决于范围按钮,点击该过滤器。我有2个按钮的范围,名字和姓氏。默认姓按钮被选择。下面是我如何我的数据添加到mutablearray,

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"]]]; 
    }

我的搜索code,

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;
}

以上code可以很好地用于搜索用户数据数组,但我不知道我将如何改变code,这样根据名字,姓氏范围按钮,它会显示结果。我怎么会挂钩的按钮来搜索栏,使其只取决于什么范围栏按钮时显示的结果。在此先感谢..

The above code works well for searching the userData array, but 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..

推荐答案

您需要做两件事情:
 1.查看searchBar.selectedScopeButtonIndex的价值 - 这会告诉你,如果你需要搜索的名字或姓氏。
 2.根据范围按钮,你要搜索要么每个数组项目的第一部分和第二部分。有很多方法可以做到这一点。也许最简单的就是保持2并行阵列,firstNames和lastNames,你从tmpDic填充。然后实际搜索,你既可以遍历firstNames或lastNames,或者做一个(INT J = 0; J< firstNames计数]; J ++),并得到[firstNames objectAtIndex:J]和比较,为您的临时字符串。如果有任何字符串匹配,将其添加到您的结果数组。

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.

这篇关于帮助与搜索栏按钮范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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