views:

22

answers:

1

I am trying to have a tableview that leads to another tableview whose list items depend on the first view. I have the database set up properly and my problem is that when I select one item on the first page and go to the second view for the first time it works fine but when I return to the first page and go back to the second view again the list still has the old values loaded with new ones. Is there a way to destroy the array and recreate it every time the second view is loaded? Where would I do this?

Here is where my array is initialized in the delegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

 [self copyDatabaseIfNeeded];

 organArray = [[NSMutableArray alloc] init]; 
 procedureArray = [[NSMutableArray alloc] init];

 [Organ getInitialDataToDisplay:[self getDBPath]];

    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

Here is where the data is added to the array in my custom class:

    + (void) getDatabase:(NSString *)dbPath WithOrganID:(NSNumber *)organID
{

 RadiologyAppAppDelegate *appDelegate = (RadiologyAppAppDelegate *)[[UIApplication sharedApplication] delegate];

 if(sqlite3_open([dbPath UTF8String],&database) == SQLITE_OK){

  const char *sql = "select * from Organ";
  sqlite3_stmt *selectstmt;

  if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK){

   while (sqlite3_step(selectstmt) == SQLITE_ROW) {

    NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);

    Procedure *procedureObj = [[Procedure alloc] initWithPrimaryKey:primaryKey];
    procedureObj.procedureName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt,1)];
    procedureObj.procedureID = sqlite3_column_int(selectstmt,0);

    [appDelegate.procedureArray addObject: procedureObj];

    [procedureObj release];
   }
  }
 }
 else
  sqlite3_close(database);
}

Finally, here is my view controller:

    - (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];
    }

 Procedure *procedureObj = [appDelegate.procedureArray objectAtIndex:indexPath.row];
 PVCprocedureObj = appDelegate.procedureArray;

 cell.textLabel.text = procedureObj.procedureName;

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;

}

Thanks for the help!

A: 

I would pass the data-object (data to be shown in the second table) to the 2nd controller. I think this is the easiest approach and you are not showing that part. Pass the object when clicking the cell, inside: – tableView:didSelectRowAtIndexPath: and not when making the cell: -tableView:cellForRowAtIndexPath: Hope this helps

nacho4d
I am calling the method to populate the array in the didSelectRowAtIndexPath in my root view controller. Is this what you mean?
Mark Cicero
I created a method in my delegate that I call just before my array population method that re-initializes the array and it works great now. Thanks for the help!
Mark Cicero