2011-10-13 25 views
0

我想從數據庫加載數據到uipickerview。 這是我的代碼如何在iphone中從數據庫加載uipickerview值?

if (sqlite3_prepare_v2(UsersDB, query_stmt, -1, &statement, NULL) == SQLITE_OK) 
{ 
int i=0; 
while (sqlite3_step(statement) == SQLITE_ROW) 
{ 
NSLog(@"select"); 
self.coun=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 
NSLog(@"Coun = %@",coun); 

self.animals=[coun componentsSeparatedByString:@":"]; 
NSLog(@"String value=%@",self.animals); 
} 
sqlite3_finalize(statement); 
} else 
{ 
NSLog(@"not select"); 
} 

和按鈕操作的代碼是

- (IBAction)selectAnItem:(UIControl *)sender { 
//Display the ActionSheetPicker 
[ActionSheetPicker displayActionPickerWithView:sender data:self.animals selectedIndex:self.selectedIndex target:self action:@selector(itemWasSelected::) title:@"Select Country"]; 
} 

但運行負荷只有最後一個值。並非所有值都在UIpickerview中加載。如何使用此代碼加載數據庫中的所有值。任何人都可以幫助我。提前致謝。

+0

笏是烏拉圭回合的動物對象,則達Array.And你的數據庫的價值明杆怎麼會? –

+0

動物是NSArray。 coun是NSString – akk

+0

是否所有的動物值都存儲爲單個字符串在你的DB.Like動物1:anilmal2:animal3 –

回答

1

你要做這樣的,

NSArray *splitedString = [coun componentsSeparatedByString: @":"]; 
NSString *animalName; 
if ([splitedString count]>0) 
{ 
     animalName=[splitedString objectAtIndex:0]; 
     [self.animals addObject:animalName]; //here animals should be NSMutableArray 
} 
+0

這裏動物是NSArray。bcz - (IBAction)selectAnItem:(UIControl *)sender { //顯示ActionSheetPicker [ActionSheetPicker displayActionPickerWithView:發送數據:self.animals的selectedIndex:self.selectedIndex目標:自我行動:@selector(itemWasSelected::)標題:@ 「選擇國家」];} 只甲肝的NSArray – akk

+0

我認爲NSMutableArray可以通過NSArray。 –

+0

爲什麼我要告訴動物數組爲可變數組,Bcz數組內容應該動態添加 –

0

while循環中,您必須收集數組中的所有self.coun值。當你走出循環時,擁有所有的值,填充選擇器。

NSMutableArray *countries = [[NSMutableArray alloc] init]; 


while (sqlite3_step(statement) == SQLITE_ROW){ 
    self.coun=[[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 0)]; 
    NSLog(@"Coun = %@",coun); 
    [countries addObject:coun];  
    //self.animals=[coun componentsSeparatedByString:@":"]; 

    //NSLog(@"String value=%@",self.animals); 
} 
// Now you can use countries to populate in picker. 

您必須爲UIPickerView實現數據源和委託。

看起來,您正在將數據存儲在數據庫中,讀取的數據是您在self.animals中指定的數據庫。這種類型的變量名稱不匹配。爲什麼你用「:」解析?

+0

這是我的控制檯輸出 2011-10-13 16:52:54.688 Aapkeapps [3381:207]選擇 2011-10-13 16:52:54.689 Aapkeapps [3381:207 ] Coun =印度 2011-10-13 16:52:54.690 Aapkeapps [3381:207]字符串值=( 印度 ) 2011-10-13 16:52:54.690 Aapkeapps [3381:207] select 2011- Aapkeapps [3381:207]字符串值=( 美國 ) 2011-10-13 16:52:54.692 Aapkeapps [3381:207] Aapkeapps [3381:207] Coun = America 2011-10-13 16:52:54.692 16: 52:54.692 Aapkeapps [3381:207] select 2011-10-13 16:52:54.693 Aapkeapps [3381:207] Coun =英格蘭 2011-10-13 16:52:54.694 Aapkeapps [3381:207]字符串值=( 英格蘭 ) Bt只裝載一個數組 – akk

+0

什麼是您的數據庫列及其內容? – karim

+0

列名是'CountryName' 內容是印度,美國,英國 – akk

相關問題