2010-07-16 16 views
0

我想要一個tableview導致另一個tableview的列表項取決於第一個視圖。我有正確的數據庫設置,我的問題是,當我在第一頁上選擇一個項目,第一次轉到第二個視圖時,它工作正常,但是當我返回到第一個頁面並再次返回到第二個視圖時該列表仍舊具有用新值加載的舊值。有沒有辦法銷燬數組,並在每次加載第二個視圖時重新創建它?我會在哪裏做這件事?如何重置多維數組中的數組iphone應用

這裏是我的陣列正在委託初始化:

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

這裏就是數據被添加到陣列中的我的自定義類:

+ (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); 
} 

最後,這裏是我的看法控制器:

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

} 

感謝您的幫助!

回答

0

我會將數據對象(要顯示在第二個表中的數據)傳遞給第二個控制器。我認爲這是最簡單的方法,並且你沒有展示那部分。使單元格時,而不是:didSelectRowAtIndexPath方法:-tableView:的cellForRowAtIndexPath: 希望這有助於

+0

我打電話的方法來填充didSelectRowAtIndexPath方法陣列中的我的根 - :單擊單元格時 傳遞對象,裏面的tableView視圖控制器。你是這個意思嗎? – 2010-07-16 03:11:21

+0

我在我的委託中創建了一個方法,我在我的數組填充方法之前調用該方法,該方法重新初始化數組,現在它工作得很好。謝謝您的幫助! – 2010-07-16 03:19:16