搜索时在表格上加载结果

时间:2012-12-01 17:22:08

标签: ios uitableview

我不知道如何处理这个问题,我只是在学习桌子。我有下面的代码,在您键入时过滤数组。搜索栏向上移动,控制器在搜索下方变暗,但是当我键入第一个字母时,结果不会在下面生成。

这是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 个答案:

答案 0 :(得分:1)

我的猜测是下面的一个出来了:

  • 资源路径为possibleitemss - 这是一个错字吗?
  • 您是否已验证阵列已填充?
  • 你的searchDisplayController:shouldReloadTableForSearchScope:实际上是回归是吗?

换句话说,我将逐步介绍这些函数,并观察流程的表现方式,以及您的数组实际上已填充。

如果有帮助,请告诉我。