2011-09-19 35 views
0

我是(iPhone的新手)開發一款跟蹤數據使用情況(wifi,手機)的iPhone應用程序。我給用戶一個選項來添加新的計劃,這些計劃將被放入數據庫(sqlite)的表中。 我在選取器視圖中顯示計劃列表,我希望在用戶輸入新計劃後立即刷新選取器視圖中的數據。截至目前pickerview被提前 感謝下次運行:(得到更新 代碼是在這裏:如何在不運行應用程序的情況下反映數據庫中的更改?

actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0); 
    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame]; 
    pickerView.showsSelectionIndicator = YES; 
    pickerView.dataSource = self; 
    pickerView.delegate = self; 
    [actionSheet addSubview:pickerView]; 
    plans= [[NSMutableArray alloc] init]; 
    NSLog(@"Data base is entering"); 
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"antara" ofType:@"sqlite"]; 
    FMDatabase *db1 = [[FMDatabase alloc] initWithPath:path1]; 
    [db1 open]; 
    FMResultSet *fResult2= [db1 executeQuery:@"SELECT * FROM main"]; 
    NSLog(@"fresult2 ready"); 
    while ([fResult2 next]) 
    { 
     planData = [fResult2 stringForColumn:@"planName"]; 
     [plans addObject:planData]; 
     NSLog(@"The data is =%@",planData); 
    } 
    [db1 close]; 
    [pickerView selectRow:1 inComponent:0 animated:NO]; 
    //mlabel.text= [plans objectAtIndex:[pickerView selectedRowInComponent:0]]; 
    //[self setCurrentPlan:[plans objectAtIndex:[pickerView selectedRowInComponent:0]]]; 
    //[mlabel setText:[self getCurrentPlan]]; 
    currentPlan= [plans objectAtIndex:[pickerView selectedRowInComponent:0]]; 
    [plan setText:currentPlan]; 
    [pickerView release]; 
+0

請將您的代碼粘貼到您爲picker寫入數據源功能的位置。 –

+0

@rahul:我在問題 –

+0

中添加了代碼好吧,你的代碼看起來很好。問題是什麼時候將數據插入到數據庫中?如果在pickerview當前沒有顯示的情況下將數據插入數據庫,那麼這應該工作得很好。但看看你的問題描述,即使在顯示選擇器視圖時,你似乎也想重新加載數據。這個假設是否正確? –

回答

1

我認爲你需要在代碼中進行一些分離。試試這個,告訴我它是否可以工作

1)把數據從數據庫拉到一個單獨的函數的邏輯,讓我們稱之爲loadPlans。

- (void) loadPlans { 
plans= [[NSMutableArray alloc] init]; 
NSLog(@"Data base is entering"); 
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"antara" ofType:@"sqlite"]; 
FMDatabase *db1 = [[FMDatabase alloc] initWithPath:path1]; 
[db1 open]; 
FMResultSet *fResult2= [db1 executeQuery:@"SELECT * FROM main"]; 
NSLog(@"fresult2 ready"); 

while ([fResult2 next]) 
{ 
    planData = [fResult2 stringForColumn:@"planName"]; 
    [plans addObject:planData]; 
    NSLog(@"The data is =%@",planData); 
} 


[db1 close]; 
} 

2)顯然,這代碼是從您在上述因此粘貼的功能拍攝,除去從該函數這些行。

3)在正常的流程:調用上述功能

4)只要你有東西插入到數據庫中之前調用loadPlans。調用這兩個功能又出現

[self loadPlans]; 
[PickerView reloadComponent:n]; 

這一切都假定計劃的數據庫條目在同一頁上,並在同一個線程發生,讓你擁有控制權,在那裏你可以調用這些函數。如果這個假設不是真的,那麼也解釋如何以及何時將計劃添加到數據庫中,在這裏粘貼一些關於數據庫條目的代碼。

+0

好吧,我會嘗試和電話給你。非常感謝你 –

+0

我知道了,謝謝你! –

0

所有你需要做的是通過

[customPickerView reloadComponent:n] // where n = the index of the component 
觸發重建你是顯示的組件

把觸發代碼加載完成後某處

您也可以使用這樣的事情來檢查pickerView是可見的,如果你真的需要更新屏幕。

if ([screenUpdateDelegate respondsToSelector:@selector(whateverIneedToDo:)]) { 
     [screenUpdateDelegate whateverIneedToDo:withParameterIfYouLike]; 
相關問題