2012-12-01 26 views
0

我不知道我該如何處理這個問題,我只是在學習表格。我有下面的代碼,可以在您輸入時過濾數組。搜索欄向上移動,控制器在搜索下方變暗,但是當我鍵入第一個字母時,結果不會在下面生成。搜索時在表格上加載結果

這是對Apple示例項目的修改。 Tablesearch

我錯過了什麼?

而且鍵盤似乎拖到後面,當新的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 

回答

1

我的猜測是,一個下面是出:

  • 資源路徑是possibleitemss - - 是一個錯字?
  • 您是否驗證過陣列已填充?
  • 您的searchDisplayController:shouldReloadTableForSearchScope:實際返回是嗎?

換句話說,我會介入到這些函數中的每一箇中,並觀察流程的行爲如何,以及您的數組事實上是否已被填充。

讓我知道是否有幫助。