2011-10-13 54 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