Iphone UISearchDisplayController和自定义单元格
我在UITableViewController中实现了UISearchDisplayController。搜索正在进行,但UISearchDisplayController使用标准单元格创建了一个新的tableView,而我的tableView使用的是自定义单元格。另一个问题是,我使用了segue:Iphone UISearchDisplayController和自定义单元格,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我在UITableViewController中实现了UISearchDisplayController。搜索正在进行,但UISearchDisplayController使用标准单元格创建了一个新的tableView,而我的tableView使用的是自定义单元格。另一个问题是,我使用了segue: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 并且searchResultsTableView中的
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
并且searchResultsTableView中的单元格不会触发segue
如何在自定义单元格和segue工作的情况下显示搜索结果
这是代码:
...
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView){
return [nameFilteredObj count];
} else {
return [sortedObj count];
}}
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Name"];
} else {
cell.nameLabel.text = [[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Name"];
cell.countryLabel.text = [[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Country"];
cell.bottleImageView.image = [UIImage imageNamed:[[sortedObj objectAtIndex:indexPath.row] valueForKey:@"Image"]];
}
return cell;
}
//The codes for filtering the results and update of searchResultsTableView (not necessary for this question I think):
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[nameFilteredObj removeAllObjects];
for(NSDictionary *obj in sortedObj)
{
NSString *objName = [obj objectForKey:@"Name"];
NSRange range = [objName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[nameFilteredObj addObject:obj];
}}}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return NO;}
...
我使用故事板作为界面。实际上很容易做到,但我发现了许多复杂的半途而废的解决方案。使用这个简单的片段,SearchResultsTableView就会消失:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
...
self.searchDisplayController.searchResultsTableView.hidden=YES;
return YES;
}
您是否将
self.searchDislpayController.searchResultsDataSource
设置为self
?以及searchResultsDelegate
?self.searchDisplayController.searchResultsDataSource=self
。在创建搜索控制器的位置执行此操作。将其添加到创建搜索显示控制器的代码行之后。或者,如果这是在Interface Builder中完成的,则可能需要将视图控制器连接到搜索显示控制器的数据源和代理属性。我不使用IB,所以我不知道怎么做。好的,我使用IB,我想我知道怎么做,谢谢你的帮助。。尽管代表们都很好……这是怎么解决的?您所做的只是隐藏应该显示过滤结果的表视图,而显示未过滤的表视图。