Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-06-2010, 10:27 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 12
perwyl is on a distinguished road
Lightbulb Display Search Result From Database (via coding)

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!
perwyl is offline   Reply With Quote
Reply

Bookmarks

Tags
database, display, search, table view

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 336
12 members and 324 guests
Absentia, Domele, fiftysixty, givensur, heshiming, iGamesDev, linkmx, michaelhansen, PixelInteractive, raihan.zbr, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,117
Posts: 402,891
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0