在表格视图中放置搜索栏。选择结果给出错误的详细视图?
Posted
技术标签:
【中文标题】在表格视图中放置搜索栏。选择结果给出错误的详细视图?【英文标题】:Placed Search Bar in Table View. Selecting result gives wrong Detail View? 【发布时间】:2013-04-04 22:22:36 【问题描述】:我创建了一个包含自定义单元格的 TableView 控制器,并填充了来自 mysql 数据库的数据。一切都很好。
我添加了一个搜索栏,搜索效果也很好。但是,当我选择搜索结果时,它会将我带到错误的详细视图(例如,我单击“Cookies”并将我发送到“Apples”的详细视图)。我的 .m 文件中是否缺少某些内容?这是我的代码:
.m
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if (tableView == self.searchDisplayController.searchResultsTableView)
return [searchResults count];
else
return [Strains count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *strainTableIdentifier = @"StrainTableCell";
StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
if (cell == nil)
cell = [[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
if (tableView == self.searchDisplayController.searchResultsTableView)
cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"];
cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"];
NSLog(@"%@", searchResults);
else
cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"];
cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"];
return cell;
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [Strains filteredArrayUsingPredicate:resultPredicate];
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc]
initWithNibName:@"StrainDetailViewController" bundle:nil];
detailViewController.title = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
detailViewController.strainDetail = [Strains objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
【问题讨论】:
【参考方案1】:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc] initWithNibName:@"StrainDetailViewController" bundle:nil];
if (self.searchDisplayController.searchResultsTableView)
detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
detailViewController.strainDetail = [searchResults objectAtIndex:indexPath.row];
else
detailViewController.title = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
detailViewController.strainDetail = [Strains objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
【讨论】:
嗨!这修复了我的搜索栏结果,显示了正确的细节视图控制器。但是,当我实现这一点时,它会在我不搜索时从我的详细视图控制器中删除数据。例如。如果我只是从我的表格视图中进行选择,它会推送到一个空的详细视图。 由 [Strains objectAtIndex:indexPath.row] 导致的空原因返回空。我建议你不要使用两个 UITableView (实例)。如果您正在搜索,您可以更改 UITableView 的 dataSoure 并重新加载 UITableView。 不知道这两个UITableView是怎么显示的。同框? "...如果您正在搜索,您可以更改 UITableView 的数据源并重新加载 UITableView。"我该怎么做呢?我很困惑(在这方面还很陌生)。以上是关于在表格视图中放置搜索栏。选择结果给出错误的详细视图?的主要内容,如果未能解决你的问题,请参考以下文章