Hi
I have a database, a table view that can display the values from the database and a search bar at the header view of the table.
How do i allow the user to search from the database?
i have follow the tutorial (which uses interface builder) and added some codes to my application but it didn't work. (i guess is due to the adding of searchDisplayCOntroller or smth but i donnoe how to solve)
Can anyone mind taking some of ur time and look through my codes?
Controller.M
Code:
(view did load)
//create a filter list that will store products for the search results table
self.searchList = [NSMutableArray arrayWithCapacity:[self.queryList count]];
//restore search settings if there were saved in didReciveMemoryWarning
if (self.savedSearchTerm)
{
[self.searchDisplayController setActive:self.searchWasActive];
[self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
[self.searchDisplayController.searchBar setText:savedSearchTerm];
self.savedSearchTerm = nil;
}
[self.tableView reloadData];
self.tableView.scrollEnabled = YES;
Code:
- (void)viewDidDisappear:(BOOL)animated
{
// save the state of the search UI so that it can be restored if the view is re-created
self.searchWasActive = [self.searchDisplayController isActive];
self.savedSearchTerm = [self.searchDisplayController.searchBar text];
self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
}
Code:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
//hTravelv12AppDelegate *appDelegate = (hTravelv12AppDelegate *) [[UIApplication sharedApplication] delegate];
//NSLog(@"COUNT%d", appDelegate.queryList.count);
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.searchList count];
}
else
{
return queryList.count;
}
}
Code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
/*
If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
*/
TaxiQuery *taxiQuery = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
taxiQuery = [self.searchList objectAtIndex:indexPath.row];
NSLog(@"Inside search list");
}
else
{
// Configure the cell.
//hTravelv12AppDelegate *appDelegate = (hTravelv12AppDelegate *)[[UIApplication sharedApplication] delegate];
taxiQuery = (TaxiQuery *)[queryList objectAtIndex:indexPath.row];
NSLog(@"Database list");
}
cell.textLabel.text = taxiQuery.destination;
//cell.textLabel.text = @"ddddd";
NSLog(@"Destination%@", taxiQuery.destination);
if (indexPath.row == 1)
{
NSLog(@"Destination%@", taxiQuery.destination);
}
//cell.textLabel.text = @"dddddddddddd";
return cell;
}
Code:
#pragma mark -
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
/*
Update the filtered array based on the search text and scope.
*/
[self.searchList removeAllObjects]; // First clear the filtered array.
/*
Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
*/
for (TaxiQuery *taxiQuery in queryList)
{
if ([scope isEqualToString:@"All"] || [taxiQuery.destination isEqualToString:scope])
{
NSComparisonResult result = [taxiQuery.destination compare:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.searchList addObject:taxiQuery];
}
}
}
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
Controller.h
Code:
@interface SearchTQController : UITableViewController
<UISearchDisplayDelegate, UISearchBarDelegate>
{
// Database variables
NSString *databaseName;
NSString *databasePath;
// Array to store the query objects
NSMutableArray *queryList;
//search
UISearchDisplayController *searchDisplayController;
//Array: filtered search content
NSMutableArray *searchList;
// The saved state of the search UI if a memory warning removed the view.
NSString *savedSearchTerm;
NSInteger savedScopeButtonIndex;
BOOL searchWasActive;
}
please ignore the NSLog...how should i improve my code to make the search function works? I didn't use interface builder, all are achieved via coding.
ANY HELP is MUCH MUCH appreciated!