UISearchDisplayController和自定义单元格 [英] UISearchDisplayController and custom cell

查看:55
本文介绍了UISearchDisplayController和自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在UITableViewController中实现了UISearchDisplayController.搜索工作正常,但是UISearhDisplayController使用标准单元格创建了一个新的tableView,而我的tableView使用的是自定义单元格.另一个问题是我使用了segue:

I have implemented a UISearchDisplayController in the UITableViewController. The search is working, but the UISearhDisplayController creates a new tableView with a standard cell, and my tableView is using a custom cell. Another problem is that I use a segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

并且searchResultsTableView中的单元格未触发序列.

and the cells in searchResultsTableView are not triggering the segue..

如何使用自定义单元格和搜索功能显示搜索结果?

How to display the search results with custom cell and segue working?

这是代码:

...

- (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;}
...

我正在使用Storyboard作为界面.

I'm using Storyboard as interface.

推荐答案

实际上很容易做到,但是我在途中发现了许多复杂的半工作式解决方案.使用这个简单的片段,SearchResultsTableView就会消失:

Actually very easy to do, but I found many complicated half-working solutions on the way. Use this easy piece and SearchResultsTableView is GONE:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
...
self.searchDisplayController.searchResultsTableView.hidden=YES;
return YES;
}

这篇关于UISearchDisplayController和自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆