を使用してアプリに検索機能を追加しようとしてい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を介して検索バーを追加し、そのデリゲートとデータソースがテーブルビューに自動的に接続されました.
事前に助けてくれてありがとう。
編集:
これが私の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'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>