1

我无法在 UITableView 上进行搜索。我已经在 UITableViewController h 文件中声明了委托等:

@protocol searchTanksListDelegate
- (void) searchTanksList:(UISearchBar *) sender textDidChange:(NSString *) searchText;
@end

@property (weak, nonatomic) id <searchTanksListDelegate> searchDelegate;
@property (weak, nonatomic) IBOutlet UISearchBar *searchTanksList;

我在 UITableViewController viewDidLoad 函数中将 searchTanksList.delegate 设置为 self。

我已经实现了这个方法:

- (void)searchTanksList:(UISearchBar *)searchTanksList textDidChange:(NSString *)searchText
{
    if (searchText.length == 0)
    {
        self.isFiltered = false;
    }
    else {
        self.isFiltered = true;
        self.searchResults = [[NSMutableArray alloc] init];
        for (tank* thisTank in self.tanks)
        {
            NSRange nameRange = [thisTank.tankProduct rangeOfString:searchText options:NSCaseInsensitiveSearch];
            NSRange numberRange = [thisTank.tankNumber rangeOfString:searchText options:NSCaseInsensitiveSearch];
            NSRange productNumberRange = [thisTank.tankProductNumber rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (nameRange.location != NSNotFound || numberRange.location != NSNotFound || productNumberRange.location != NSNotFound)
            {
                 [self.searchResults addObject:thisTank];
            }
        }
    }
}

属性已经被合成等等 - 所以代码编译没有错误或警告......虽然我输入时没有调用 textDidChange 方法!

4

1 回答 1

4

您应该将 searchBar 声明为属性并确保将类设置为UISearchBarDelegate. 然后使用委托方法:

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

处理搜索。如果你需要,我可以给你一个完整的例子。

假设您要过滤的是数组的示例:

#pragma mark - SearchBar Delegate Methods
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{    
    [self filterContentForSearchText:searchText];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.text = @"";

    workingTableViewData = [[NSMutableArray alloc] initWithArray:originalTableViewData];
    [self.myTableView reloadData];

    [searchBar resignFirstResponder];    
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (void)filterContentForSearchText:(NSString *)searchText
{
    if (searchText && searchText.length) {
        [self.workingTableViewData removeAllObjects];    
        for (NSDictionary *dictionary in originalTableViewData)
        {
            for (NSString *thisKey in [dictionary allKeys]) {
                if ([thisKey isEqualToString:@"Key1"] || 
                        [thisKey isEqualToString:@"Key2"]) {
                    if ([[dictionary valueForKey:thisKey] rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound) {
                        [workingTableViewData addObject:dictionary];
                    }

                } // if ([thisKey isEqualToString:@"Key1"] || [thisKey isEqualToString:@"Key2"])

            } // for (NSString *thisKey in [dictionary allKeys])

        } // for (NSDictionary *dictionary in originalTableViewData)

        [self.myTableView reloadData];

    } // if (searchText && searchText.length)

}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self filterContentForSearchText:searchBar.text];
    [searchBar resignFirstResponder];
}

注意 - workingTableViewData 和 originalTableViewData 都是私有属性。originalTableViewData 是一个 NSArray,workingTableViewData 是一个 NSMutableArray。

于 2012-04-07T02:19:07.340 回答