gpt4 book ai didi

ios - 如何在 uipickerview 中获取选定行的标题和值?

转载 作者:行者123 更新时间:2023-11-29 12:43:55 25 4
gpt4 key购买 nike

在我的 iphone 项目中,我需要从 sqlite 获取数据并将其显示在 uipickerview 上,然后使用选定行的值我需要填充另一个 uipickerview

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

这就是我获取区域列表的方式

- (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(您可能认为是国家 - 城市关系)这是我的第一个问题

现在这就是我填充 pickerview 的方式

首先在 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 委托(delegate)方法

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



}

}

我想要的是 select option 标签在 html 中的作用。我需要 2 列但只显示 1 并获取所选行的值而不是文本

1- 我如何在 getRegions 函数中返回 2 列?2- 我怎样才能在 uipickerview 中只显示名称列并获取其选定行的 ID 列?

最佳答案

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

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

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

在你需要改变你委托(delegate)的功能之后

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

}

}

关于ios - 如何在 uipickerview 中获取选定行的标题和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24155829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com