我的搜索栏不起作用

时间:2013-12-14 04:07:43

标签: uisearchbar searchbar

我想制作一个搜索栏。这段代码有什么问题吗?

- (void)viewDidLoad
{
[super viewDidLoad];
PFQuery *query = [PFUser query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error %@, %@", error, [error userInfo]);

    }
    else {
        self.allUsers = objects;
        [self.tableView reloadData];
    }
}];

self.searchedUsers = [NSMutableArray arrayWithCapacity:[self.allUsers count]];
self.searchUsers = [[UISearchBar alloc]init];
self.doSearch = [[UISearchDisplayController alloc]initWithSearchBar:self.searchUsers        contentsController:self];
self.doSearch.delegate = self;
self.doSearch.searchResultsDataSource = self;
self.doSearch.searchResultsDelegate = self;



}

- (void) fileterContentForSearchText:(NSString *)searchText {
[self.searchedUsers removeAllObjects];
for (NSDictionary *item in self.allUsers)
{
    NSComparisonResult result = [[item objectForKey:@"name"] compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0,       [searchText length])];
    if (result == NSOrderedSame) {
        [searchedUsers addObject:item];
    }
}
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString];
return YES;
}

有人可以修改此代码吗?我遵循了大量的教程,但没有一个有效。我真的很感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您在方法名称上输入了错误:fileterContentForSearchText:

应该是:filterContentForSearchText: