1

I have a search bar added in the user interface. The Searchbar is already set, and works, but I can't figure out how to make it search content in the UITableView. Can I just remove the items that do not start with the characters typed in the search bar??

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (searchBar.text.length < 1) {
    return;
}
else {
    // Do search stuff here



}}

This code works, the else function is called. But I don't know how to do it. I don't want to create a whole new array just for that.

4

1 回答 1

3

我给你一个更好的。此示例将根据用户类型进行搜索。如果您的数据非常庞大,您可能希望按照- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar您最初的计划来实施它。不幸的是,您需要第二个数组,但很容易在表格中围绕它编写代码:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if(searchText.length == 0)
    {
        isFiltered = FALSE;
    }
    else
    {

        isFiltered = true;
        if (filteredTableData == nil)
            filteredTableData = [[NSMutableArray alloc] init];
        else 
            [filteredTableData removeAllObjects];

        for (NSString* string in self.masterSiteList)
        {
            NSRange nameRange = [string rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
            if(nameRange.location != NSNotFound)
            {
                [filteredTableData addObject:string];
            }
        }
    }
    [self.tableView reloadData];
}

然后更新您的委托方法以在var 设置为 YESfilteredTableData时显示数组的数据而不是常规数组。isFiltered

于 2012-07-02T23:13:06.057 回答