加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – UISearchDisplayController和自定义单元格

发布时间:2020-12-14 19:00:53 所属栏目:百科 来源:网络整理
导读:我在UITableViewController中实现了一个UISearchDisplayController.搜索工作正常,但UISearhDisplayController创建一个带有标准单元格的新tableView,而我的tableView正在使用自定义单元格.另一个问题是我使用了一个segue: - (void)prepareForSegue:(UIStoryb
我在UITableViewController中实现了一个UISearchDisplayController.搜索工作正常,但UISearhDisplayController创建一个带有标准单元格的新tableView,而我的tableView正在使用自定义单元格.另一个问题是我使用了一个segue:

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

我正在使用Storyboard作为界面.

解决方法

实际上很容易做到,但我发现了许多复杂的半工作解决方案.使用这个简单的部分,SearchResultsTableView是GONE:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读