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 09-11-2010, 12:40 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 6
bruckerrlb is on a distinguished road
Default selecting search results from table view with sqlite

I have found a bunch of tutorials on searching with the iphone, a lot of them have been very helpful but I can't seem to find what I'm looking for which is a list of results in a table view from a sqlite table, and then drill down into those results. For example, the example might be connected to a database, and it's showing results for a name, and when you click on the name it takes you to a view controller with what you clicked, which is the name. I'm trying to take it a step farther and when you click on the name, it sends the view controller being pushed a name id (for example) instead of the name itself. I've been on this for more hours and days than I care to admit so any tips are much appreciated.

I have a database that brings up results from a sqlite table that stores contact information like name and a contact id. My method to get the data queries the database and then stores the results in the app delegate:
Code:
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
	MyAppDelegate *appDelegte = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	//initialize Account Name array
	appDelegte.lastName = [[NSMutableArray alloc] init];
	appDelegte.firstName = [[NSMutableArray alloc] init];
	appDelegte.contactID = [[NSMutableArray alloc] init];
//code to get db results and store it in app delegate
This is initialized in the root view controller, which is a table view that displays options to search by address, name, phone, etc.
Code:
[LastName getInitialDataToDisplay:[appDelegte getDBPath]];
My names view controller shows all names by default and all names that are searched, are searched for in a range. The cellForRowAtIndexPath method looks like
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"CellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
	
	
	if(searching) {
		MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
		cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row];
		
		
	}
	else {
		MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
		
		
		
		NSUInteger row = [indexPath row];
		cell.textLabel.text = [appDelegate.lastNameSearch objectAtIndex:row];
		
		cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	}
	
    return cell;
	
}
copyListofItems is the array of names searched and it's created as follows:
Code:
- (void) searchTableView {
	
	NSString *searchText = searchBar.text;
	
	NSMutableArray *searchArray = [[NSMutableArray alloc] init];
	
	
	for (NSDictionary *dictionary in listOfItems)
	{
		NSArray *array = [dictionary objectForKey:@"lastNameKey"];
		
		[searchArray addObjectsFromArray:array];
		
	}
	//for searched items, need to do same for contactid
	for (NSString *sTemp in searchArray)
	{
		NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
		
		
		if(titleResultsRange.length > 0) {
			
			[copyListOfItems addObject:sTemp];
				
		
	}

	[searchArray release];

}
My didSelectRowAtIndexPath looks pretty much the same, but this is where i'm having the problem. If the user hasn't searched, the contactid I had originally stored in the app delegate gets called, but I was never getting the contactid from the step above because I'm not sure how to get that and then call it in the method below
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	
		NSInteger cou =[copyListOfItems count];
	
	MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	//if they've searched
	if(cou != nil) {
		NSUInteger row = [indexPath row];
		//this is the problem here, if copyListOfItems has a value, it means the table has been
		//searched, but I have no way of knowing what the contactid is
		delegate.viewFirstNameHolder = [copyListOfItems objectAtIndex:row];
		
	}
	//if they are selecting at index row
	else {
		NSUInteger row = [indexPath row];
		//if they haven't searched, the contactID is the same as the row that they are selecting
		delegate.viewFirstNameHolder = [delegate.contactID objectAtIndex:row];
		NSLog(@"ID is %@", delegate.viewFirstNameHolder);
		
		
	}
	//push the main account view controller to see the results (results are controlled by contactID)
	ViewAccountController *vac = [[ViewAccountController alloc]
								  initWithNibName:@"ViewAccountController" bundle:nil];
	
	self.viewAccountController = vac;
	[vac release];
	viewAccountController.title = [NSString stringWithFormat:@"Name Results"];
	[delegate.accountNavController pushViewController:viewAccountController animated:YES];
	
	
}
So that is my issue, can anyone recommend better logic to use, example code, examples of a fix or any other thoughts surrounding how to get this to work?

Thanks
bruckerrlb is offline   Reply With Quote
Reply

Bookmarks

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: 344
13 members and 331 guests
bignoggins, carlandrews, cgokey, flamingliquid, givensur, hzwegjxg, ilmman, jenniead38, linkmx, mraalex, PixelInteractive, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
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 11:56 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0