【问题标题】:UISearchBar calls API everytime a letter is typed and not on Searchbutton click每次输入字母而不是单击搜索按钮时,UISearchBar 都会调用 API
【发布时间】:2012-02-02 17:28:17
【问题描述】:

我正在做一个学生项目。

此时我们的应用程序可以调用 API 并重新加载数据,但前提是我调用 textDidChange 中的 XMLParser。

含义:每次在 UISearchBar 中键入一个字母时,它都会正确调用和重新加载。我希望调用和重新加载仅在用户单击搜索按钮时发生,但在 textDidChange 中工作的相同代码在 searchBarSearchButtonClicked 中不起作用。

相反.. 该方法仅在按下搜索按钮 (good) 时调用 API,接收与 textDidChange 相同的信息 (good) 但不会重新加载 UITableView (坏)

我已经到处搜索了我的问题的答案,所以我想我会发布一个问题:)

我遇到的所有示例仅显示了当用户键入(联系人列表)时如何显示与用户条件匹配的数组的内容,但没有显示如何使用搜索按钮正确重新加载 uitableView。

为什么相同的代码在委托方法中正确重新加载

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

但不在

- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar?

正如我所说,当单击 searchButton 时,NSLog 会打印出正确的数据以加载 uitableview,所以这不是问题。

任何人都可以在这里指出正确的方向吗? :)

我的 cellForRowAtIndexPath:

`

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *uniqueIdentifier = @"searchCell";

    SearchCell *cell = nil;

    cell = (SearchCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];

    if(!cell)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[SearchCell class]]) {
                cell = (SearchCell *)currentObject;
                break;
            }
        }
    }

    Search *currentSearch = [[searchxmlParser searchhits] objectAtIndex:indexPath.row];

    cell.track_label.text = [currentSearch track];
    cell.artist_label.text = [currentSearch artist];

    return cell;
}

请求的委托方法:

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

    NSString *searchurl = [NSString stringWithFormat:@"http://ws.spotify.com/search/1/track?q=%@", [self urlEncodeValue:searchText]];

    xmlParser = [[SearchXMLParser alloc] loadXMLByURL:searchurl];

    [self.tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {

    NSString *searchurl = [NSString stringWithFormat:@"http://ws.spotify.com/search/1/track?q=%@", [self urlEncodeValue:theSearchBar.text]];

    xmlParser = [[SearchXMLParser alloc] loadXMLByURL:searchurl];

    [self.tableView reloadData];

    [theSearchBar setShowsCancelButton:NO animated:YES];
    [theSearchBar resignFirstResponder];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[searchxmlParser searchhits] count];
}

感谢您的宝贵时间! :)

【问题讨论】:

  • 是的,感谢您在我编辑您的问题时花费的时间
  • 您使用搜索显示控制器吗?还是不行?
  • 谢谢 :) 是的,我正在使用搜索显示控制器 :)
  • 索引路径处的行单元格看起来如何?您是否测试是否显示女巫表视图?添加一些代码..
  • @terenteIonutAlexandru 兄弟,你没有得到所有这些。 @_AlexanderNorway 我建议在这两种委托方法中发布您的代码,以便我们可以看到您在那里做错了什么。

标签: iphone api uitableview uisearchbar


【解决方案1】:

您应该使用UISearchDisplayController

在您的 h 文件中,您应该声明 2 个数组,一个带有原始数据源,一个带有 filtredValues。在 viewDidLoad 中分配它们并在 dealloc 中释放。

在 cellForRowAtIndexPat 和 umberOfRowsInSection 中,您应该测试是否显示 tableView 并返回所需的值。

if(tableView == self.searchDisplayController.searchResultsTableView){
// search view population
} else {
// all data view population
}

通过这种方法,您可以通过过滤 dataSource 数组并将值放入 filtredArray 来使用实时搜索。

【讨论】:

  • 非常感谢您为我指明了正确的方向 :) 这篇文章正是我需要意识到我实际上使用两个 tableViews 而不是一个。使用上面提供的代码,很明显我的代码重新加载了 tableview 而不是重新加载 searchResultsTableView。(单击 SearchButton 时它切换到 searchResultsTableView)。搜索现在完美运行:)
【解决方案2】:

您需要将自己设置为搜索栏的代理,并覆盖代理搜索栏代理searchDisplayController:shouldReloadTableForSearchString:的默认行为。

默认实现会在每次按键时进行搜索。您所要做的就是在方法中返回NO

- (BOOL) searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString {
        return NO;
}

【讨论】:

  • 那没有帮助。看来我只能从 textDidChange 内部重新加载 tableview。随着您提到的方法返回为否,我无法重新加载任何内容,但如果我使用是,并且只在 textDidChange 中重新加载;然后发生这种情况:我写了一个歌曲名称,按回车键,我得到了我应该得到的数据(NSLog),但表格视图中什么都没有出现。在表格视图中获得我想要的结果;在单击搜索按钮空间后,我实际上必须进入搜索字段并按空格键。
【解决方案3】:

您是否正确实现了 numberOfSectionsInTableView 和 numberOfRowsInSection ?

请出示这两种方法的代码。

加法:

如果你放

NSLog(@" number of rows in section = %d", [[searchxmlParser searchhits] count]);

它输出什么?

【讨论】:

  • 我已将它们添加到原始帖子中:)
  • 我现在已经解决了这个问题 :) (查看上面帖子的评论)。但是..我现在需要在执行搜索后清除 tableview。关于我如何做到这一点的任何提示? :)
  • 假设您的意思是清空 searchResultsTableView 以返回完整列表,如果您以编程方式创建 searchResultsTableview,您可以在不再需要时放弃它,并在每次需要时重新创建它。
  • 完整列表是我不需要的。它应该只在按下搜索按钮时显示 searchResultsTableview。解决此问题的最佳方法是在表格顶部添加一个黑色视图,该视图在用户按下搜索按钮之前一直隐藏。无论如何我都需要这样做,因为我需要在打字时掩盖“无结果”消息。感谢您的输入! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 2020-10-25
  • 2019-07-11
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多