【问题标题】:UISearchDisplayController and custom cellUISearchDisplayController 和自定义单元格
【发布时间】:2012-10-30 22:19:39
【问题描述】:

我在 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 作为界面。

【问题讨论】:

  • 您是否将self.searchDislpayController.searchResultsDataSource 设置为self?还有searchResultsDelegate?
  • self.searchDisplayController.searchResultsDataSource = self。在您创建搜索控制器的地方执行此操作。
  • 将它添加到创建搜索显示控制器的代码行之后。或者,如果这是在 Interface Builder 中完成的,您需要将视图控制器连接到搜索显示控制器的数据源和委托属性。我不使用 IB,所以我不知道该怎么做。
  • 好吧,我用的是 IB,我想我知道怎么做,谢谢你的帮助.. 虽然代表们很好..

标签: iphone objective-c ios xcode


【解决方案1】:

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

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

【讨论】:

  • 解决方案如何?您所做的只是隐藏应该显示过滤结果的表格视图并显示未过滤的表格视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多