UISearchBar 搜索两个数组

发布于 2025-01-05 18:45:03 字数 1117 浏览 0 评论 0原文

我有一个搜索栏,用于搜索数组,并用结果更新 UITableView。表格视图是书籍列表,包含标题和作者:

标题和作者

现在,搜索栏仅搜索标题,但我想让它也搜索作者。这是我的搜索代码(我从 http://blog.webscale.co 获得.in/?p=228)。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [tableData removeAllObjects];// remove all data that belongs to previous search
    if([searchText isEqualToString:@""]||searchText==nil){
        [tableView reloadData];
        return;
    }

    for(NSString *name in dataSource){
         NSInteger counter = 0;

        //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSRange r = [[name lowercaseString] rangeOfString:[searchText lowercaseString]];
        if(r.location != NSNotFound)
            [tableData addObject:name];


            counter++;
    }
        //[pool release];


    [tableView reloadData];

dataSource

是包含标题的 NSMutable 数组。包含作者的数组称为“author”。 “tableData”是存储应出现在屏幕上的单元格(包含正在搜索的术语的单元格)的数组。

非常感谢,

卢克

I Have a search bar that searches an array, and updates a UITableView with the results. The table view is a list of books, with titles and authors:

The titles and authors

Right now, the search bar only searches the titles but I would like to make it search the authors as well. Here is the search code I have (I got it from http://blog.webscale.co.in/?p=228).

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [tableData removeAllObjects];// remove all data that belongs to previous search
    if([searchText isEqualToString:@""]||searchText==nil){
        [tableView reloadData];
        return;
    }

    for(NSString *name in dataSource){
         NSInteger counter = 0;

        //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSRange r = [[name lowercaseString] rangeOfString:[searchText lowercaseString]];
        if(r.location != NSNotFound)
            [tableData addObject:name];


            counter++;
    }
        //[pool release];


    [tableView reloadData];

}

dataSource is the NSMutable Array that contains the titles. the array that contains the authors is called "author". "tableData" is the array that stores the cells that should appear on the screen (the cells that contain terms being searched for).

Thanks so much,

Luke

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昵称有卵用 2025-01-12 18:45:03

我将通过创建带有键值对的 NSDictionary 来修改 dataSource 数组以包含标题和作者(Book 类会更好)。

//Do this for each book
NSDictionary * book = NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    title, @"TITLE", author, @"AUTHOR", nil];
[dataSource addObject:book];

之后,您可以更改搜索方法以使用 NSDictionary。

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

    [tableData removeAllObjects];

    if(searchText != nil && ![searchText isEqualToString:@""]){

        for(NSDictionary * book in dataSource){
            NSString * title = [book objectForKey:@"TITLE"];
            NSString * author = [book objectForKey:@"AUTHOR"];

            NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]];
            NSRange authorRange = [[author lowercaseString] rangeOfString:[searchText lowercaseString]];

            if(titleRange.location != NSNotFound || authorRange.location != NSNotFound)
                [tableData addObject:book];
            }

    }

    [tableView reloadData];
}

请注意,使用此方法您将更改 cellForRowAtIndexPath 方法以使用 NSDictionary 而不是标题字符串。

I would modify the dataSource array to contain both the titles and authors by creating an NSDictionary with key value pairs (a Book class would be better).

//Do this for each book
NSDictionary * book = NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
    title, @"TITLE", author, @"AUTHOR", nil];
[dataSource addObject:book];

After that you can change your search method to work with the NSDictionary instead.

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

    [tableData removeAllObjects];

    if(searchText != nil && ![searchText isEqualToString:@""]){

        for(NSDictionary * book in dataSource){
            NSString * title = [book objectForKey:@"TITLE"];
            NSString * author = [book objectForKey:@"AUTHOR"];

            NSRange titleRange = [[title lowercaseString] rangeOfString:[searchText lowercaseString]];
            NSRange authorRange = [[author lowercaseString] rangeOfString:[searchText lowercaseString]];

            if(titleRange.location != NSNotFound || authorRange.location != NSNotFound)
                [tableData addObject:book];
            }

    }

    [tableView reloadData];
}

Note using this method you would have the change your cellForRowAtIndexPath method to work with the NSDictionary and not the title strings.

泛泛之交 2025-01-12 18:45:03
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText
{
if ([searchText length]==0)
{
  temp_array1 =[array_Main1 mutableCopy];
  temp_array2 =[array_Main2 mutableCopy];
  temp_array3 =[array_Main3 mutableCopy];
}
 else
{
  [temp_array1 removeAllObjects];
  [temp_array2 removeAllObjects];
  [temp_array3 removeAllObjects];
   int g = 0;
   for (int i=0; i< array_Main1.count;i++)
    {
        NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (Range1.location != NSNotFound ||  Range2.location != NSNotFound ||  Range3.location != NSNotFound )
        {
            [temp_array1 addObject:[array_Main1 objectAtIndex:g]];
            [temp_array2 addObject:[array_Main2 objectAtIndex:g]];
            [temp_array3 addObject:[array_Main3 objectAtIndex:g]];

        }
        g++;
    }
}
[table reloadData];
}
-(void)searchBar:(UISearchBar *)searchBar1 textDidChange:(NSString *)searchText
{
if ([searchText length]==0)
{
  temp_array1 =[array_Main1 mutableCopy];
  temp_array2 =[array_Main2 mutableCopy];
  temp_array3 =[array_Main3 mutableCopy];
}
 else
{
  [temp_array1 removeAllObjects];
  [temp_array2 removeAllObjects];
  [temp_array3 removeAllObjects];
   int g = 0;
   for (int i=0; i< array_Main1.count;i++)
    {
        NSRange Range1 = [[array_Main1 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range2 = [[array_Main2 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        NSRange Range3 = [[array_Main3 objectAtIndex:i] rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (Range1.location != NSNotFound ||  Range2.location != NSNotFound ||  Range3.location != NSNotFound )
        {
            [temp_array1 addObject:[array_Main1 objectAtIndex:g]];
            [temp_array2 addObject:[array_Main2 objectAtIndex:g]];
            [temp_array3 addObject:[array_Main3 objectAtIndex:g]];

        }
        g++;
    }
}
[table reloadData];
}
喜爱皱眉﹌ 2025-01-12 18:45:03
- This is Helpful when you search from Dictionary.

NSMutableArray *contentList;
NSMutableArray *filteredContentList;
BOOL isSearching;

// firstSection is array which already filled.
// contentList array for value of particular key
// filteredContentList is search array from actual array.

  - (void)searchTableList {
        NSString *searchString = searchBar.text;

        NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"frame_code beginswith[c] %@", searchString];
        NSArray *filteredArr = [firstSection filteredArrayUsingPredicate:filterPredicate];
        if(contentList.count > 0)
            [contentList removeAllObjects];
        [filteredContentList addObjectsFromArray:filteredArr];
    }

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar1 {

        if ([searchBar1.text length] != 0)
            isSearching = YES;
        else
            isSearching = NO;
    }

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        NSLog(@"Text change - %d",isSearching);

        //Remove all objects first.
        [filteredContentList removeAllObjects];

        if([searchText length] != 0) {
            isSearching = YES;
            [self searchTableList];
        }
        else {
            isSearching = NO;
        }
        [tblFrameList_SComplete reloadData];
    }

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"Cancel clicked");
    }

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"Search Clicked");
        [self searchTableList];
    }
- This is Helpful when you search from Dictionary.

NSMutableArray *contentList;
NSMutableArray *filteredContentList;
BOOL isSearching;

// firstSection is array which already filled.
// contentList array for value of particular key
// filteredContentList is search array from actual array.

  - (void)searchTableList {
        NSString *searchString = searchBar.text;

        NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"frame_code beginswith[c] %@", searchString];
        NSArray *filteredArr = [firstSection filteredArrayUsingPredicate:filterPredicate];
        if(contentList.count > 0)
            [contentList removeAllObjects];
        [filteredContentList addObjectsFromArray:filteredArr];
    }

    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar1 {

        if ([searchBar1.text length] != 0)
            isSearching = YES;
        else
            isSearching = NO;
    }

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        NSLog(@"Text change - %d",isSearching);

        //Remove all objects first.
        [filteredContentList removeAllObjects];

        if([searchText length] != 0) {
            isSearching = YES;
            [self searchTableList];
        }
        else {
            isSearching = NO;
        }
        [tblFrameList_SComplete reloadData];
    }

    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"Cancel clicked");
    }

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        NSLog(@"Search Clicked");
        [self searchTableList];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文