Reputation: 766
i'am trying to accomplish a search bar on a UITableView populated with a NSMutableArray. The problem is that the search bar delegate methods are obviously not working. When i type something in the search bar i just got no answer, even the NSLog(@"test") doesn't trigger, and that's driving me crazy. if i can get any help please. (Rookie ObjC level here ^^)
Here is my code (minus all the parsing xml part) : (.h)
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface MetamorphViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate>{
IBOutlet UITableView * newsTable;
UISearchBar *searchBar;
UISearchDisplayController *searchDisplayController;
UIActivityIndicatorView * activityIndicator;
CGSize cellSize;
NSXMLParser * rssParser;
NSMutableArray * stories;
NSArray * newStories;
NSMutableArray *filteredStories;
NSString *dataType;
NSString *idName;
BOOL *uploaded;
BOOL isFiltered;
NSMutableDictionary * item;
NSString * currentElement;
NSMutableString * currentTitle, * currentDate, * currentSummary, * currentLink, * currentModule, *currentOwner, *currentOwnername;
}
- (void)parseXMLFileAtURL:(NSString *)URL typeOfModule:(NSString *)typeOfData;
@end
(.m)
- (void)viewDidLoad {
self.navigationItem.rightBarButtonItem = self.editButtonItem;
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,70,320,44)];
[searchBar setShowsScopeBar:YES];
[searchBar setScopeButtonTitles:[[NSArray alloc] initWithObjects:@"OBJ",@"C", nil]];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
[searchDisplayController setSearchResultsDelegate:self];
self.tableView.tableHeaderView = searchBar;
[self.tableView reloadData];
self.tableView.scrollEnabled = YES;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSLog(@"test");
//NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchBar];
//newStories = [stories filteredArrayUsingPredicate:filterPredicate];
//NSLog(@"newStories %@", newStories);
}
Upvotes: 4
Views: 9089
Reputation: 5417
You don't need to be the delegate of the UISearchBar; the UISearchDisplayDelegate method called searchDisplayController:shouldReloadTableForSearchString:
is the method you want to implement. What you need to do in this method is filter your data according to the search string and then return YES to reload the tableview. UISearchDisplayDelegates can also receive callbacks for will/did end/begin searching, some of these methods may need to be implemented as well. For example, undo your data filtering in searchDisplayControllerWillEndSearch:
so that your all your data reappears after canceling a search.
Upvotes: 0
Reputation: 25459
You haven't set delegate to your searchBar object. After that line:
[searchBar setShowsScopeBar:YES];
add:
searchBar.delegate = self;
Don't forget to add <UISearchBarDelegate>
protocol.
Upvotes: 4
Reputation: 137
Is it possible that you forgot to set the delegate for searchBar
. I am not completely sure, but I think searchDisplayController.delegate = self;
won't do.
Upvotes: 1