UISearchBar 搜索两个数组 [英] UISearchBar search two arrays

查看:99
本文介绍了UISearchBar 搜索两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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 是包含标题的 NSMutable 数组.包含作者的数组称为作者".tableData"是存储应该出现在屏幕上的单元格(包含正在搜索的术语的单元格)的数组.

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).

非常感谢,

卢克

推荐答案

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

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

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

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

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

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

这篇关于UISearchBar 搜索两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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