我正在尝试根据给定的搜索重新加载TableViewController,问题是它没有显示找到的结果,它只显示白色单元格。我的代码是这样的:
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时,我的搜索条件得到了肯定的结果,但它根本没有加载任何单元格。有什么建议吗?
答案 0 :(得分: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