【问题标题】:UISearchDisplayController - How to implement and get it workingUISearchDisplayController - 如何实现并让它工作
【发布时间】:2011-04-21 22:46:54
【问题描述】:

我已经花了几个小时阅读了几十个不同的博客和其他关于这个的问题,但我陷入了僵局。

我的问题是如何让这件事发挥作用。我已经在 IB 中通过代码进行了尝试。

当我在 IB 中尝试它时,即使我将它拖到(UITableViewController 的)表视图上并且它“捕捉”到标题所属的表上,搜索栏也永远不会显示。但是搜索栏永远不会出现。 (这是一个子问题,有人知道为什么吗?)

所以我尝试仅在代码中进行设置。我得到它分配,分配委托和数据源,但我从来没有得到那个说“没有结果”的表格视图。栏出现了,我可以输入,但它总是只显示一个空表,即使我知道应该有结果。我怀疑它没有显示正确的表格,因为没有显示“无结果”文本的单元格。

 - (void)viewDidLoad {

    [super viewDidLoad];

    names = [[NSMutableArray alloc] initWithObjects:@"Michael", @"Dwight", @"Jim", @"Andy", @"Ryan", @"Creed", @"Darryl", @"Kevin", @"Oscar", @"Gabe", nil];
    results = [[NSMutableArray alloc] init];

    UISearchBar *sb = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    sb.tintColor = [UIColor greenColor];
    searchTable.tableHeaderView = sb;

    UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
    [self setSearchDisplayController:sdc];
    [sdc setDelegate:self];
    [sdc setSearchResultsDataSource:self];}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.searchTable) {
        // normal table view population
        return [names count];
    }
    if(tableView == self.searchDisplayController.searchResultsTableView){
        // search view population
        return [results count];
    }
    return 0;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }

    if (tableView == self.searchTable) {
        // normal table view population
        cell.textLabel.text = [names objectAtIndex:indexPath.row];
    }
    if(tableView == self.searchDisplayController.searchResultsTableView){
        // search view population
    }

    cell.textLabel.textColor = [UIColor blueColor];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    if (tableView == self.searchTable) {
        // normal table view population
    }
    if(tableView == self.searchDisplayController.searchResultsTableView){
        // search view population
    }   
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
    [results removeAllObjects]; // First clear the filtered array.

    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */
    for (NSString *str in names)
    {
        NSComparisonResult result = [str compare:controller.searchBar.text options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [controller.searchBar.text length])];
        if (result == NSOrderedSame)
        {
            [results addObject:str];
        }
    }
}

Any help would be so very much appreciated. I would add a bounty, but as you can see, I've got "Newb" written on my forehead still :).

【问题讨论】:

  • 不看一些代码很难知道出了什么问题。

标签: iphone objective-c uitableview uisearchdisplaycontroller


【解决方案1】:

我看到了几个可能的故障点:

1) 您是否在其 .h 文件中将您的视图控制器设置为委托?示例:

@interface myViewController : UIViewController <UISearchBarDelegate,UISearchDisplayDelegate,UITableViewDataSource,UITableViewDelegate>{

2) 我不知道这是您的完整代码,还是只是相关的 sn-ps,但如果是完整代码,那么在您看到任何数据显示之前还有几个漏洞。您需要在其中的某处添加一个 tableview 以显示结果并填写 cellForRowAtIndexPath 方法中的第二个 if 语句。

我建议您查看 Apple 提供的 TableSearch 示例代码,这应该会让您朝着正确的方向前进。

【讨论】:

  • 我一直在关注那个样本,我不知道他们是如何让它工作的。我查看了我在 IB 中的关系,它们都完全相同。我确实有那些代表在那里。所以我必须从结果中构建表格,searchController 不这样做?
  • 从示例代码来看,您似乎没有将搜索结果放入表中if(tableView == self.searchDisplayController.searchResultsTableView){ cell.textLabel.text = [results objectAtIndex:indexPath.row]; }
  • 因此,如果我必须构建该表,那么使用 searchDisplayController 有什么意义。我的意思是如果我必须做所有这些,它有什么好处,只是很酷的动画?
  • 目的是为了能够搜索到一个tableview,所以假设你已经有一个tableview可以搜索了。如果您搜索的不是预先存在的 tableview,那么您可能不会从使用它中获得太多收益。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 1970-01-01
  • 2012-08-14
  • 2012-02-11
相关资源
最近更新 更多