これにどのようにアプローチできるかわかりません。テーブルについて学んでいます。入力時に配列をフィルタリングする以下のコードがあります。検索バーが上に移動し、コントローラーが検索の下で暗くなりますが、最初の文字を入力しても結果が下に生成されません。
これは、Apple のサンプル プロジェクトを変更したものです。テーブル検索
私は何が欠けていますか?
また、新しいVCのロード時にキーボードが遅れているようです よろしくお願いします
// itemsSearchViewController.m
#import "itemsSearchViewController.h"
#import "SearchRecipeViewController.h"
#import "firstAppDelegate.h"
@interface itemsSearchViewController ()
@end
@implementation itemsSearchViewController
@synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive;
@synthesize delegate;
@synthesize itemsToPass;
/*
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
#pragma mark -
#pragma mark Lifecycle methods
*/
- (void)viewDidLoad
{
//[super viewDidLoad];
self.title = @"items Search";
//write names of itemss to array
NSString *path =[[NSBundle mainBundle] pathForResource:@"possibleitemss" ofType:@"plist"];
NSArray *content = [NSArray arrayWithContentsOfFile:path];
self.listContent =[NSArray arrayWithArray:content];
if([content count] == 0)
{
NSLog(@"nsma is empty");
}
NSLog(@"list contents%@", listContent);
// NSLog(@"list content = %@", listContent);
// create a filtered list that will contain products for the search results table.
self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];
// restore search settings if they were saved in didReceiveMemoryWarning.
if (self.savedSearchTerm)
{
[self.searchDisplayController setActive:self.searchWasActive];
[self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
[self.searchDisplayController.searchBar setText:savedSearchTerm];
self.savedSearchTerm = nil;
}
[self.tableView reloadData];
self.tableView.scrollEnabled = YES;
}
- (void)viewDidUnload
{
//self.filteredListContent = nil;
}
- (void)viewDidDisappear:(BOOL)animated
{
// save the state of the search UI so that it can be restored if the view is re-created
self.searchWasActive = [self.searchDisplayController isActive];
self.savedSearchTerm = [self.searchDisplayController.searchBar text];
self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
}
- (void)dealloc
{
[listContent release];
[filteredListContent release];
[super dealloc];
}
#pragma mark -
#pragma mark UITableView data source and delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
/*
If the requesting table view is the search display controller's table view, return the count of
the filtered list, otherwise return the count of the main list.
*/
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.filteredListContent count];
}
else
{
return [self.listContent count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
}
/*
If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
*/
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
If the requesting table view is the search display controller's table view, configure the next view controller using the filtered content, otherwise use the main list.
*/
}
#pragma mark -
#pragma mark Content Filtering
//as user types this is happening
-(void) filterResults:(NSString *)searchText{
NSMutableArray *test = [NSMutableArray arrayWithArray:self.listContent];
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (int i=0; i<[test count]; i++) {
NSString *stringResult = [test objectAtIndex:i];
NSComparisonResult result = [stringResult compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame){
[self.filteredListContent addObject:stringResult];
}
}
[self.filteredListContent sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];//sort alphabetically
NSLog(@"filtered results = %@",self.filteredListContent);
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{items
[self filterResults:searchString];
//[self filterContentForSearchText:searchString scope:
// [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (IBAction)itemsSelected: (id)sender{
if ( self.delegate != nil && [self.delegate respondsToSelector:@selector(showSearchRecipeView)] ) {
[self.delegate showSearchRecipeView];
}
}
@end