我有一个UIPickerView控件来从sqlite填充它。
这是.h文件中的singleSelectedData属性:
@property (nonatomic,strong)NSMutableArray *singleSelectedData;
这是我的ViewDidload方法:
- (void)viewDidLoad
{
[super viewDidLoad];
_singleSelectedData=[[NSMutableArray alloc] init];
[self loadQuestions];
}
这是我创建uipickerview的loadQuestions方法。它还加载了许多控件。在这里,您只能看到选择器视图部分:
else if (inputType==SingleSelectionQuestion)
{
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,656,768,44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(pickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)];
[barItems addObject:doneBtn];
pickerToolbar.hidden=YES;
myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,700,768,260)];
myPicker.tag = [[[dataQuestions objectAtIndex:k] valueForKey:@"ids"] integerValue];
myPicker.showsSelectionIndicator = TRUE;
myPicker.dataSource = self;
myPicker.delegate = self;
myPicker.hidden = YES;
[myPicker setBackgroundColor:[UIColor grayColor]];
myPicker.tag=[[[dataQuestions objectAtIndex:k] valueForKey:@"ids"] integerValue];
UIButton *buttonControl = [UIButton buttonWithType:UIButtonTypeCustom];
buttonControl.tag = [[[dataQuestions objectAtIndex:k] valueForKey:@"ids"] integerValue];
[buttonControl setBackgroundImage:[UIImage imageNamed:@"combobox.png"] forState:UIControlStateNormal];
[buttonControl setTitle:@"Seçiniz" forState:UIControlStateNormal];
[buttonControl setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
buttonControl.frame = CGRectMake(30,sectionY + mainCategoryHeight+subCategoryHeight+questionLabel.frame.size.height +offsetQuestion,433,38);
[buttonControl addTarget:self action:@selector(selectSingleChoice:) forControlEvents:UIControlEventTouchUpInside];
NSMutableArray *choices=[[NSMutableArray alloc] initWithArray:[[DBHelper getSharedInstance:[LoggedinUser sharedCenter].dbFileName] getQuestionChoicesByQuestionID:[[dataQuestions objectAtIndex:k] valueForKey:@"ids"]]];
_singleSelectedData= [choices valueForKey:@"choiceName"];
// singleSelectedData=[[NSMutableArray alloc] initWithObjects:@"aaa",@"vvv", nil];
[pickerToolbar setItems:barItems animated:YES];
[self.scrlview addSubview:pickerToolbar];
[self.scrlview addSubview:buttonControl];
}
编辑:
这是我从sqlite中选择的方法:
- (NSMutableArray*) getQuestionChoicesByQuestionID:(NSString *)questionID
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"select CHOICE_ID,QUESTION_ID,NAME FROM T_PROGRAM_QUESTION_CHOICE as p INNER JOIN T_QUESTION_OPTION_CHOICE as c on c.ID=p.CHOICE_ID WHERE QUESTION_ID=%@",questionID];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *questionIDs = [[NSMutableArray alloc]init];
NSMutableArray *choiceIDs = [[NSMutableArray alloc]init];
NSMutableArray *choiceNames = [[NSMutableArray alloc]init];
NSMutableArray *mainArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement,nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *choiceID = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[choiceIDs addObject:choiceID];
NSString *questionID = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
[questionIDs addObject:questionID];
NSString *choiceName = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[choiceNames addObject:choiceName];
}
sqlite3_reset(statement);
if(questionIDs !=nil)
{
for (int index = 0; index<questionIDs.count; index++) {
NSMutableDictionary *dataAddDic =[[NSMutableDictionary alloc]init];
[dataAddDic setValue:[choiceIDs objectAtIndex:index] forKeyPath:@"choiceID"];
[dataAddDic setValue:[questionIDs objectAtIndex:index] forKeyPath:@"questionID"];
[dataAddDic setValue:[choiceNames objectAtIndex:index] forKeyPath:@"choiceName"];
[mainArray addObject:dataAddDic];
}
if(mainArray.count>0)
{
return mainArray;
}
else
{
return nil;
}
}
else
{
NSLog(@"problem1: %@",statement);
return nil;
}
}
else
{
NSLog(@"problem2: %@",statement);
}
}
else
{
NSLog(@"problem3");
}
return nil;
}
如您所见,我将singleSelectedData设置为数组。阵列不是空的。但不知何故,我的选择器视图在视图加载时没有项目。如果我手动设置对象的singleSelectedData数组。项目显示。我不明白有什么区别,想知道如何从sqlite获取项目并显示它?