首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将搜索栏添加到使用带有NSDictionaries的数组的TableView

如何将搜索栏添加到使用带有NSDictionaries的数组的TableView
EN

Stack Overflow用户
提问于 2013-05-29 18:44:18
回答 1查看 255关注 0票数 1

到目前为止,我所做的是让搜索栏工作。

代码语言:javascript
复制
- (void)viewDidLoad
{

[self makeData];

[super viewDidLoad];

self.mySearchBar.delegate = self;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;

//Feedback button code here
UIBarButtonItem *feedback;
feedback = [[UIBarButtonItem alloc] initWithTitle:@"Contact"   style:UIBarButtonItemStylePlain
target:self action:@selector(showEmail:)];
self.navigationItem.leftBarButtonItem = feedback;


[self.tableView reloadData];
self.tableView.scrollEnabled = YES;

}

-(void) makeData; {

list = [[NSMutableArray alloc] init];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Acceleration", @"name" ,
                @"acceleration.png", @"image" ,
                @"Acceleration", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Alternating Current", @"name" ,
                @"alternating current.png", @"image" ,
                @"Alternating Current", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Ampere", @"name" ,
                @"ampere.png", @"image" ,
                @"Ampere", @"description" ,nil]];

[list addObject:[[NSMutableDictionary alloc]
                initWithObjectsAndKeys:@"Amplitude", @"name" ,
                @"amplitude.png", @"image" ,
                @"Amplitude", @"description" ,nil]];


}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
    isFiltered = FALSE;
}
else
{
    isFiltered = TRUE;
    filteredList = [[NSMutableArray alloc] init];

    for (NSString *str in list) {
        NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (stringRange.location != NSNotFound) {
            [filteredList addObject:str];
        }
    }
}
[self.myTableView reloadData];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.myTableView reloadData];
}

然而,每次我尝试在应用程序中键入某些内容时,都会崩溃,并收到以下消息:

代码语言:javascript
复制
2013-05-29 11:36:09.734 PhysEX[4770:c07] -[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0
2013-05-29 11:36:09.735 PhysEX[4770:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance 0x9468cd0'

如何让我的搜索工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-30 00:30:19

textDidChange委托中的以下代码不正确:

代码语言:javascript
复制
for (NSString *str in list) {
    ...
}

这是因为列表数组中的对象不是NSString,而是NSDictionary。因此会出现错误消息,指出NSDictionary无法识别选择器rangeOfString:options:,因为该选择器用于NSString实例。

相反,您需要搜索NSDictionary值。

代码语言:javascript
复制
for (NSDictionary *theDictionary in list) {

    for (NSString *key in theDictionary) {
        NSString *value = [theDictionary objectForKey:key];
        NSRange stringRange = [value rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (stringRange.location != NSNotFound) {
            [filteredList addObject:theDictionary];
            break;
        }
   }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16811791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档