2012-03-18 8 views
0

特定の検索に基づいてTableViewControllerをリロードしようとしていますが、見つかった結果が表示されず、白いセルのみが表示されるという問題があります。私のコードは次のようである:UISearch searchDisplayController:shouldReloadTableForSearchString:リロードしない

FooTableViewController.h

#import <UIKit/UIKit.h> 

#import "Foo.h" 

@interface FooTableViewController : UITableViewController<UISearchBarDelegate, UISearchDisplayDelegate> 
{ 
    NSMutableArray * foos; 

    UISearchBar * searchBar; 
    UISearchDisplayController * searchDisplayController; 
    NSMutableArray * searchFoos; 
} 

@property NSMutableArray * foos; 

@end 

FooTableViewController.m

#import "FooTableViewController.h" 

#import "FooCell.h" 

@interface FooTableViewController() 
@end 

@implementation FooTableViewController 

@synthesize foos; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

// NSMutableArray foos is populated 
- (void) populateFoo; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self populateFoo]; 
    searchFoos = [[NSMutableArray alloc] init]; 

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 

    self.tableView.tableHeaderView = searchBar; 

    [self.tableView reloadData]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int sections = 1; 
    if(tableView == self.tableView) 
    { 
     sections = [foos count]; 
    } 
    // si estamos en vista de búsqueda 
    if(tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     sections = [searchFoos count]; 
    } 

    return sections; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    FooCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[FooCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    if(tableView == self.tableView) 
    { 
     Foo * foo = (Foo *)[foos objectAtIndex:indexPath.row]; 
     cell.uilabel_name.text = foo.name; 
     cell.uilabel_date.text = foo.date; 
    } 
    if(tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     Foo * foo = (Foo *)[searchFoos objectAtIndex:indexPath.row]; 
     cell.uilabel_name.text = foo.name; 
     cell.uilabel_date.text = foo.date; 
    } 

    return cell; 
} 

#pragma mark - Search bar delegates 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [searchFoos removeAllObjects]; 

    for (Foo * foo in foos) 
    { 
     if([foo.name rangeOfString:searchString options:NSCaseInsensitiveSearch].location != NSNotFound) 
     { 
      [searchFoos addObject:e]; 
     } 
    } 

    [self.searchDisplayController.searchResultsTableView reloadData]; 
    return YES; 
} 

@end 

私はsearchDisplayControllerをデバッグする場合:shouldReloadTableForSearchString、私は私の検索用語のための肯定的な結果を得るが、それは単にdoesnのどの細胞もロードしないでください。助言がありますか?

答えて

0
#import "FooTableViewController.h" 

#import "FooCell.h" 

@interface FooTableViewController() 
@end 

@implementation FooTableViewController 

@synthesize foos; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

// NSMutableArray foos is populated 
- (void) populateFoo; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self populateFoo]; 
    searchFoos = [[NSMutableArray alloc] init]; 

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 

    self.tableView.tableHeaderView = searchBar; 

    [self.tableView reloadData]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int rows = 1; 
    if(tableView == self.tableView) 
    { 
     rows = [foos count]; 
    } 
    else 
    { 
     rows = [searchFoos count]; 
    } 

    return rows; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    FooCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[FooCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    if(tableView == self.tableView) 
    { 
     Foo * foo = (Foo *)[foos objectAtIndex:indexPath.row]; 
     cell.uilabel_name.text = foo.name; 
     cell.uilabel_date.text = foo.date; 
    } 
    else 
    { 
     Foo * foo = (Foo *)[searchFoos objectAtIndex:indexPath.row]; 
     UILabel * newLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 50, 50); 
     newLabel.text = foo.name; 
     [foo addSubview: newLabel]; 
    } 

    return cell; 
} 

#pragma mark - Search bar delegates 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [searchFoos removeAllObjects]; 

    for (Foo * foo in foos) 
    { 
     if([foo.name rangeOfString:searchString options:NSCaseInsensitiveSearch].location != NSNotFound) 
     { 
      [searchFoos addObject:e]; 
     } 
    } 
    return YES; 
} 

@end 
関連する問題