如何使用searchController并使用object-c显示ios8.0的tableview

时间:2016-06-12 15:11:31

标签: ios objective-c uitableview

#pragma mark - UISearchResultsUpdating
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    [searchResults removeAllObjects];

    NSString *searchString = searchController.searchBar.text;

       if([searchString length]>0){
       for(NSUInteger i =0; i < titles.count; i++){

NSDictionary *titleAndIndex = [NSDictionary dictionaryWithObjectsAndKeys:[titles objectAtIndex:i], @"title",[ChNames objectAtIndex:i], @"ChName",[checkon objectAtIndex:i], @"checkon",[NSNumber numberWithUnsignedInteger:i], @"index",nil];

[searchResults addObject:titleAndIndex];
NSLog(@"filteredArray2: %@", titleAndIndex);

}

[searchResults filterUsingPredicate:[NSPredicate                              predicateWithFormat:@"title contains[c] %@",searchString]];

}

 [self.tableView reloadData];

}


#pragma mark - UISearchBarDelegate

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

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,它可以帮助你。

pragma mark - UISearchResultsUpdating

 - (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

[searchResults removeAllObjects];

self.tableView.allowsMultipleSelectionDuringEditing = YES;


NSString *searchString = searchController.searchBar.text;
for(NSUInteger i =0; i < titles.count; i++){
    NSDictionary *titleAndIndex = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [titles objectAtIndex:i], @"title",nil];
    [searchResults addObject:titleAndIndex];
    //      NSLog(@"filteredArray2: %@", titleAndIndex);



}
[searchResults filterUsingPredicate:[NSPredicate
                                     predicateWithFormat:@"title contains[c] %@",searchString]];
dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];


});
}