Reputation: 81
Please excuse my ignorance with this question but I'm still trying to get to grips with the basics and have been banging my head against a wall for a couple of days over this now.
I essentially have an NSArray
that I'm using to populate a UITableView
that I would like users to be able to search through – and which filters the results that contain characters in the search criteria, as the users types.
There are quite a few tutorials out there for hooking up UITableViews
and UISearchDisplayControllers
but they all use the xib file and I want to add everything programatically.
I have the UITableView
, with its contents shown in a UIView
when the user taps a button, I appear to have added the UISearchBar
at the top of the same view, and I 'think' I've added a UISearchDisplayController
. However, I can't seem to get all three playing nicely.
Here's what I have so far
- (IBAction)createSearchList:(id)sender {
//Make the list
AAPjsonParse * makeTheFullList = [[AAPjsonParse alloc] init];
fullListOfRecipesForTableView = [makeTheFullList generateFullListOfRecipes];
fullListForSearchResultsInTableView = fullListOfRecipesForTableView;
recipeListForTable = [fullListForSearchResultsInTableView allKeys];
recipeListForTable = [recipeListForTable sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
//Create the UITableView
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 300, 724) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
//Add the TableView to the view
[self.searchView addSubview:tableView];
//Create a UISearchBar for the TableView
UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
//Set the searchBar as the delegate
searchBar.delegate = self;
//Add the searchBar to the UITableView
[self.searchView addSubview:searchBar];
//Create the searchViewController
UISearchDisplayController * searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
//Set the dataSource for the search as self
searchController.searchResultsDataSource = self;
//Set the results delegate as self – this is the view that's overlaid onto the full listing view (I think?)
searchController.delegate = self;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [recipeListForTable count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @"RecipeCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString * cellTitle = [recipeListForTable objectAtIndex:indexPath.row];
cell.textLabel.text = cellTitle;
return cell;
}
I've also got some stuff at the top of my implementation file to store various bits
{
NSArray * recipeListForTable;
NSMutableArray * recipeListForSearch;
int selectedRecipeIndex;
NSDictionary * fullListOfRecipesForTableView;
NSMutableDictionary * fullListForSearchResultsInTableView;
UITableView * tableView;
}
Upvotes: 2
Views: 5840
Reputation: 1412
This code will add a searchbar to your tableview
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tblView.frame), 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// searchBar.tintColor = [UIColor lightGrayColor];
searchBar.placeholder = @"What are you eating today...";
searchBar.delegate = self;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
[self.tblView setTableHeaderView:searchBar];
After adding searchbar check how to pass data and set delegate and datasource, I would suggest you to go through this Tutorial.
Upvotes: 1