如何在uipickerview中获取所选行的标题和值?

时间:2014-06-11 06:17:16

标签: ios objective-c sqlite uipickerview

在我的iphone项目中我需要从sqlite获取数据并在uipickerview上显示然后选择行的值我需要填写另一个uipickerview

在我的表T_REGION中有两列ID和NAME

这就是我如何获得REGION列表

- (NSArray*) getRegions
{

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{

    NSString *querySQL = [NSString stringWithFormat:@"select ID,NAME from T_REGION"];
    const char *query_stmt = [querySQL UTF8String];
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];
    if (sqlite3_prepare_v2(database,
                           query_stmt, -1, &statement, NULL) == SQLITE_OK)
    {
        while (sqlite3_step(statement) == SQLITE_ROW)
        {



            NSString *name = [[NSString alloc] initWithUTF8String:
                              (const char *) sqlite3_column_text(statement, 1)];
            [resultArray addObject:name];


        }
        sqlite3_reset(statement);

        return resultArray;
    }
    else
    {
        NSLog(@"problem");
    }
}
return nil;
}

如你所见,我不能填充ID列到数组只有名称列返回我需要ID列来填充另一个uipickerview(你可能认为国家 - 城市关系) 这是我的第一个问题

现在这就是我填写挑选者视图的方式

首先在viewDidLoad方法中得到数组:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self getdata];
}

- (void)getdata {
 dataRegion = [[NSArray alloc] initWithArray: [[DBHelper getSharedInstance:@"2_program.db"]getRegions]];


}

和我的uipickerview委托方法

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView.tag == 1)
    return [dataRegion count];
else
    return 0; //[countryTypes count];

}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView.tag == 1)
    return [dataRegion objectAtIndex:row];
else
    return @"";


}


 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:  (NSInteger)component
 {
if (pickerView.tag == 1) {
    NSString *categoryType  = [dataRegion objectAtIndex:[pickerView selectedRowInComponent:0]];
    [btnBolge setTitle:categoryType forState:UIControlStateNormal];

}else {



}

  }

我想要的是选择标签在html中做什么。我需要2列但只显示1并获得所选行的值而不是文本

1-如何在getRegions函数中返回2列? 2-如何在uipickerview中仅显示名称列并获取其所选行的id列?

2 个答案:

答案 0 :(得分:1)

试试这个..可能会有所帮助..

像这样你可以将两个数组添加为字典

 NSMutableArray *nameArray = [[NSMutableArray alloc]initWithObjects:@"Come",@"Say",@"Go", nil];
    NSMutableArray *regionArray = [[NSMutableArray alloc]initWithObjects:@"Hi",@"Hello",@"Bye", nil];


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

    for (int index = 0; index<regionArray.count; index++) {
        NSMutableDictionary *dataAddDic = [[NSMutableDictionary alloc]init];
        [dataAddDic setValue:[nameArray objectAtIndex:index] forKeyPath:@"name"];
        [dataAddDic setValue:[regionArray objectAtIndex:index] forKeyPath:@"region"];
        [mainArray addObject:dataAddDic];
    }
    NSLog(@"Data Print %@",mainArray);

您需要更改委派功能

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView.tag == 1)
    return [mainArray count];
else
    return 0; //[countryTypes count];

}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView.tag == 1)
    return  [[mainArray objectAtIndex:row] valueForKey:@"region"];
else
    return @"";


}

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:  (NSInteger)component
 {
if (pickerView.tag == 1) {
    NSString *categoryType  = [[mainArray objectAtIndex:row] valueForKey:@"name"];
    [btnBolge setTitle:categoryType forState:UIControlStateNormal];

}else {

 }

 }

答案 1 :(得分:0)

const char *dbpath = [databasePath UTF8String];  
if (sqlite3_open(dbpath, &database) == SQLITE_OK)  
{  
    NSString *querySQL = [NSString stringWithFormat:@"select ID,NAME from T_REGION"];  
    const char *query_stmt = [querySQL UTF8String];  
    NSMutableArray *resultArray = [[NSMutableArray alloc]init];  
    if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)  
    {  
        while (sqlite3_step(statement) == SQLITE_ROW)  
        {  
            NSString *id = [[NSString alloc] initWithUTF8Strin  
                (const char *) sqlite3_column_text(statement, 0)];  

            NSString *name = [[NSString alloc] initWithUTF8String:  
                (const char *) sqlite3_column_text(statement, 1)];  

            NSDictionary *dict = @{id:name};  
            [resultArray addObject:dict];  

        }  
        sqlite3_reset(statement);  

        return resultArray;  
        }  
        else  
        {  
            NSLog(@"problem");  
        }  
    }  
    return nil;  

}  
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component  
{  

    if (pickerView.tag == 1){  
        NSDictionary *region = [dataRegion objectAtIndex:row];  
        NSArray *keys = [region allKeys];  
        return region[[keys[0]];  
    }  
    else{  
        return @"";  
    }  

}  

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:   (NSInteger)component  
{  
    if (pickerView.tag == 1) {  
        NSDictionary *region  = [dataRegion objectAtIndex:row];  
        NSArray *keys = [region allKeys];  
        [btnBolge setTitle:region[keys[0]] forState:UIControlStateNormal];  
    }else {  

    }  
}