0

我正在尝试使用在我的应用程序中添加搜索功能NSpredicate。我试图让它发挥作用,但似乎看不出问题出在哪里。我重新创建了一个演示应用程序来展示我所做的事情。一切都显示在模拟器上,没有任何警告,但是在搜索栏中输入文本时,没有结果。搜索栏表格视图中没有显示任何内容。我正在使用搜索栏和搜索显示控制器对象,而且我的原始数据源是一个简单的 plist,其中包含字典,每个字典都有 3 个字符串。除了搜索,一切都很好。有人可以看看我的代码,看看我哪里出错了吗?

这是我的 .h 文件:

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController <UISearchBarDelegate>

@property (strong, nonatomic) NSArray *content;
@property (strong, nonatomic) NSMutableArray *searchResults;

@end

这是 .m 文件:

#import "TableViewController.h"
#import "DetailViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController

@synthesize content = _content;
@synthesize searchResults = _searchResults;

-(NSArray *)content
{
if (!_content) {
    _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];
}
return _content;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

_searchResults = [[NSMutableArray alloc] init];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [self.searchResults count];

} else {
    return [self.content count];

}
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

[_searchResults removeAllObjects];

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] '%@'",searchText];

[_searchResults addObjectsFromArray:[_content filteredArrayUsingPredicate:resultPredicate]];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
                           scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                  objectAtIndex:[self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]];

return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [_searchResults objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [_searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"city"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"state"];
}

return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
    [self performSegueWithIdentifier: @"showDetails" sender: self];
}
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

if ([segue.identifier isEqualToString:@"showDetails"]) {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    DetailViewController *DVC = [segue destinationViewController];

    if ([self.searchDisplayController isActive]) {

    DVC.cityImageString = [_searchResults objectAtIndex:indexPath.row];
    DVC.cityTextString = [_searchResults objectAtIndex:indexPath.row];
    } else {

        DVC.cityImageString = [[self.content objectAtIndex:indexPath.row] valueForKey:@"cityImage"];
        DVC.cityTextString = [[self.content objectAtIndex:indexPath.row] valueForKey:@"cityText"];
    }

}
}

@end

ps我使用的是xcode 4.6 ios 6,我认为这无关紧要,可能与我的问题无关,而且我已经通过IB添加了搜索栏,它的委托和数据源自动连接到tableview。

感谢一堆提前帮助。

编辑:

这是我的 plist 的结构,这是一个非常简单的结构,我认为它可能会帮助有人为我提供答案。

     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"   "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
       <plist version="1.0">
         <array>
      <dict>
    <key>city</key>
    <string>New York</string>
    <key>state</key>
    <string>NY</string>
    <key>cityText</key>
    <string>This is the description of the city that goes in the tex view just testing.</string>
    <key>cityImage</key>
    <string>acolon.png</string>
       </dict>
       <dict>
    <key>city</key>
    <string>Los Angeles</string>
    <key>state</key>
    <string>CA</string>
    <key>cityText</key>
    <string>This the second item&apos;s textview</string>
    <key>cityImage</key>
    <string>Piedirosso.jpg</string>
     </dict>
      <dict>
    <key>city</key>
    <string>Chicago</string>
    <key>state</key>
    <string>IL</string>
    <key>cityText</key>
    <string>here is the text view description for the third item.</string>
    <key>cityImage</key>
    <string>acolon.png</string>
</dict>
    </array>
    </plist>
4

2 回答 2

1

干得好!但是检查最终代码,我发现如果您从 searchResults 中选择一个单元格,则始终显示第一个单元格,例如,对于搜索“s”结果:“Sandiago”和“Sprienfield”,如果选择“Sprienfield”,则详细视图将去圣地亚哥。我找不到错误。

于 2013-05-27T19:17:41.403 回答
0

你的问题是这一行的谓词

NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] '%@'",searchText];

您的 _content 数组包含不支持包含运算符的 NSDictionaries。此外,单引号/双引号会导致 %@ 按字面意思使用。(来自文档:)Single or double quoting variables (or substitution variable strings) cause %@, %K, or $variable to be interpreted as a literal in the format string and so prevent any substitution。将其更改为:

NSPredicate *pred = [NSPredicate predicateWithFormat: @"SELF['city'] contains[cd] %@ OR SELF['state'] contains[cd] %@ OR SELF['cityText'] contains[cd] %@", searchText, searchText, searchText];

(假设您要搜索所有三个值:city、state、cityText)

于 2013-04-26T08:23:25.410 回答