0
我想重新加載基於給定搜索的TableViewController,問題是它沒有顯示找到的結果,它只顯示白色單元格。我的代碼是這樣的:UISearch searchDisplayController:shouldReloadTableForSearchString:not reloading
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,爲我的搜索字詞積極的結果,但它只是沒有按不加載任何單元格。有什麼建議麼?