iPhone-UITableView出现问题-重新加载数据&;没有命中

iPhone-UITableView出现问题-重新加载数据&;没有命中,iphone,uitableview,uisearchbar,Iphone,Uitableview,Uisearchbar,我使用的是苹果的示例代码: 在本例中,UITableView在启动时获取内容列表。单击UISearchBar并键入,内容列表将被过滤,同时检查ScopeBar的范围 我必须将这种“即时搜索”重建为“正常搜索”:一开始我没有TableView的数据。用户应该点击搜索栏,键入一些内容,按下“搜索”按钮,搜索请求将发送到Web服务器。Web服务器的响应将被放入TableView中,用户可以切换范围以过滤结果集。更改搜索栏的值不会过滤结果列表。仅按“搜索”键即可启动搜索请求 我获取示例代码并重新构建它

我使用的是苹果的示例代码:

在本例中,UITableView在启动时获取内容列表。单击UISearchBar并键入,内容列表将被过滤,同时检查ScopeBar的范围

我必须将这种“即时搜索”重建为“正常搜索”:一开始我没有TableView的数据。用户应该点击搜索栏,键入一些内容,按下“搜索”按钮,搜索请求将发送到Web服务器。Web服务器的响应将被放入TableView中,用户可以切换范围以过滤结果集。更改搜索栏的值不会过滤结果列表。仅按“搜索”键即可启动搜索请求

我获取示例代码并重新构建它(底部的源代码)。 但我有两个问题

  • 在最初调用SearchViewController时(使用TabBar、SearchBar、ScopeBar、TableView),一切正常。有一个空的桌面视图。但是,点击搜索栏,只输入一个字符,就会有一条消息说“没有点击”。我怎么能避免呢?只有当用户按“搜索”并且确实没有匹配项时,才会显示此消息
  • 我的第二个问题是:键入“hello”并按“Search”,TableView不会列出结果。如果单击“中止”或其他作用域,则会列出结果。所以一定有什么东西像丢失的“重新加载” 我希望有人能帮助我。 非常感谢您的问候

    我的源代码:

    @implementation SearchViewController
    
    @synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive;
    
    - (void)viewDidLoad {
        // restore search settings if they were saved in didReceiveMemoryWarning.
        if (self.savedSearchTerm) {
            [self.searchDisplayController setActive:self.searchWasActive];
            [self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
            [self.searchDisplayController.searchBar setText:savedSearchTerm];
            self.savedSearchTerm = nil;
        }
    }
    
    - (void)viewDidUnload {
        // Save the state of the search UI so that it can be restored if the view is re-created.
        self.searchWasActive = [self.searchDisplayController isActive];
        self.savedSearchTerm = [self.searchDisplayController.searchBar text];
        self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
        self.filteredListContent = nil;
    }
    
    - (void)dealloc {
        [listContent release];
        [filteredListContent release];
        [super dealloc];
    }
    
    - (void)setData {
        self.listContent = [NSMutableArray arrayWithCapacity:3];
        [self.listContent addObject:[SearchObjects itemWithType:@"AAA" name:@"Test1"]];
        [self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test2"]];
        [self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test3"]];
    
        // create a filtered list
        self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];
        [self.tableView reloadData];
        self.tableView.scrollEnabled = YES;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list.
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            return [self.filteredListContent count];
        } else {
            return [self.listContent count];
        }
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *kCellID = @"cellID";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    
        /* If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list. */
        SearchObjects *searchObject = nil;
        if (tableView == self.searchDisplayController.searchResultsTableView) {
            searchObject = [self.filteredListContent objectAtIndex:indexPath.row];
        } else {
            searchObject = [self.listContent objectAtIndex:indexPath.row];
        }
        cell.textLabel.text = searchObject.name;
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // HERE IS THE SOURCE CODE FOR PUSHING TO THE NEXT VIEW
    }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
        // DO SOME CALCULATIONS… AND THE setData METHOD IS CALLED
    }
    
    - (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
        /* Update the filtered array based on the search text and scope. */
        [self.filteredListContent removeAllObjects]; // First clear the filtered array.
    
        /* Search the main list for whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. */
        for (SearchObjects *searchObject in listContent) {
            if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) {
            NSComparisonResult result = [searchObject.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
                if (result == NSOrderedSame) {
                    [self.filteredListContent addObject:searchObject];
                }
            }
        }
    }
    
    - (void)filterContentForScope:(NSString*)scope {
        /* Update the filtered array based on the search text and scope. */
        [self.filteredListContent removeAllObjects]; // First clear the filtered array.
    
        /* Search the main list for whose type matches the scope (if selected); add items that match to the filtered array. */
        for (SearchObjects *searchObject in listContent) {
            if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) {
                [self.filteredListContent addObject:searchObject];
            }
        }
    }
    
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
        [self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];    
        // Return YES to cause the search result table view to be reloaded.
        return YES;
    }
    
    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
        [self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
        // Return YES to cause the search result table view to be reloaded.
        return YES;
    }
    @end
    

    对于第一个问题,您应该为搜索栏设置一个代理,然后实现
    –searchBarSearchButtonClicked:
    ,并将搜索代码放入其中。您可能还必须实现其他功能,如
    –searchBartextdidediting:
    –searchBar:textDidChange:
    ,并确保它们不执行搜索


    对于第二个问题,您可能希望使用委托再次从
    –searchBarSearchButtonClicked:
    重新加载tableView,以确保它发生在您已经搜索之后。您可以使用
    [tableView reloadData]
    来完成此操作。

    问题已解决,请参见注释。

    嗯,我不明白。我已经有了一个方法“searchBarSearchButtonClicked:”。在本例中,我向服务器发送请求,并使用“setData”方法将数据设置到TableView并执行重新加载数据,但这不起作用。您是否覆盖了searchBarTextDidChange以防止可能重新加载数据?我解决了问题。我设置了一个标志,只有当标志为true(表示搜索已完成)时,我才返回YES;在委托方法searchDisplayController中:控制器应重新加载Tablefor。。。另一个问题是列表内容。它会重新加载数据,但重点是filteredContentList!嗨,蒂姆,我正在做一些类似的事情,有同样的问题,但我一直无法解决它。我尝试设置一个标志,正如您在这里所描述的,并且只有在收到服务器的响应时,才在这两个委托方法中返回YES。但是,我没有看到在查询到服务器之后调用这些方法。如何使用筛选结果重新加载表?这是由服务器的响应触发的吗?此外,我没有看到您在上面的代码中描述的第二个问题(使用重载数据)。