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