我已经花了数小时阅读几十个不同的博客和其他人q在这里关于这个,所以,但我陷入了僵局。UISearchDisplayController - 如何实现并使其工作
我的问题是你如何使这件事情的工作。我已经在IB和代码中试过了。
当我在IB中尝试它时,即使将它拖到表格视图(UITableViewController的)上,它也不会显示,并且它会“捕捉”到表头所在的表格中。但搜索栏永远不会显示。 (这是一个子问题,任何人都知道为什么?)
所以我试着在代码中设置。我得到它分配,分配委托和数据源,但我从来没有得到那个表示“没有结果”的表视图。酒吧出现了,我可以输入它,但它总是显示一张空桌子,即使我知道应该有结果。我怀疑它没有显示正确的表格,因为单元格显示“没有结果”文本。失败的
- (void)viewDidLoad {
[super viewDidLoad];
names = [[NSMutableArray alloc] initWithObjects:@"Michael", @"Dwight", @"Jim", @"Andy", @"Ryan", @"Creed", @"Darryl", @"Kevin", @"Oscar", @"Gabe", nil];
results = [[NSMutableArray alloc] init];
UISearchBar *sb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
sb.tintColor = [UIColor greenColor];
searchTable.tableHeaderView = sb;
UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
[self setSearchDisplayController:sdc];
[sdc setDelegate:self];
[sdc setSearchResultsDataSource:self];}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.searchTable) {
// normal table view population
return [names count];
}
if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
return [results count];
}
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
if (tableView == self.searchTable) {
// normal table view population
cell.textLabel.text = [names objectAtIndex:indexPath.row];
}
if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
}
cell.textLabel.textColor = [UIColor blueColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.searchTable) {
// normal table view population
}
if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
[results removeAllObjects]; // First clear the filtered array.
/*
Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
for (NSString *str in names)
{
NSComparisonResult result = [str compare:controller.searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [controller.searchBar.text length])];
if (result == NSOrderedSame)
{
[results addObject:str];
}
}
}
Any help would be so very much appreciated. I would add a bounty, but as you can see, I've got "Newb" written on my forehead still :).
如果没有看到一些代码,很难知道发生了什么问题。 – conmulligan 2011-04-21 22:51:50