iphone - UISearchBar 在每次输入字母时调用 API,而不是在单击搜索按钮时调用 API

标签 iphone api uitableview uisearchbar

我正在做一个学生项目。

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

含义:每次在 UISearchBar 中输入字母时,它都会正确调用并重新加载。我希望仅当用户单击“搜索”按钮时才进行调用和重新加载,但在 textDidChange 中工作的相同代码在 searchBarSearchButtonClicked 中不起作用。

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

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

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

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

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

请求的委托(delegate)方法:

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

感谢您的宝贵时间! :)

最佳答案

您应该使用 UISearchDisplayController .

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

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

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

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

关于iphone - UISearchBar 在每次输入字母时调用 API,而不是在单击搜索按钮时调用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9096617/

相关文章:

php - 用户名不是用户表facebook api的成员

swift - UITableView ContentInset 和 ContentOffset

iOS indexPathsForSelectedRows 和初始选择

iphone - 在 ios 中绘制 splinter 图的问题(核心图)

iphone - 在 UITextView 中垂直居中文本

iphone - 从 XCode 在设备上运行时禁用调试日志记录

iphone - 在 iOS 中实现背景音频并排除故障

sql - 寻找包装在用户定义的 SQL 函数中的外部 SQL 存储过程中的任何 OS/400 API 的工作示例

c# - Web API 的静态 api key

swift - 尝试将输出绑定(bind)到 RxSwift 中的 tableviewCell UI 元素