0

我不确定如何解决这个问题,我只是在学习表格。我有下面的代码,可以在您键入时过滤数组。搜索栏向上移动,控制器在搜索下方变暗,但是当我输入第一个字母时,结果不会在下方生成。

这是对 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
4

1 回答 1

1

我的猜测是以下之一:

  • 资源路径是possibleitemss——这是一个错字吗?
  • 您是否已验证阵列已被填充?
  • searchDisplayController:shouldReloadTableForSearchScope:真的回来了吗?

In other words, I'd step into each of these functions and watch the that the flow is behaving how you expected, and that your arrays are in fact populated.

Let me know if that helps.

于 2012-12-02T16:16:02.913 回答