以编程方式添加uipickerview及其数据

时间:2014-07-22 13:26:18

标签: ios objective-c uipickerview

在我的视图中确实加载了一个for循环,它从sqlite中选择数据并动态生成uipickerview

 for (int k=0; k<dataQuestions.count; k++)
{
        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"];



        [pickerToolbar setItems:barItems animated:YES];
        [self.scrlview addSubview:pickerToolbar];
        [self.scrlview addSubview:buttonControl];


    }

所以这是我的代码,但问题是它将最后一项的数据加载到所有uipickerviews 因为我只有一个数组

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{

    return _singleSelectedData.count;


}

编辑: 我的选择器视图委托方法:

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
label.text = [_singleSelectedData objectAtIndex:row];
return label;
}

所有uipickerviews必须显示自己的数据。任何建议将不胜感激

1 个答案:

答案 0 :(得分:0)

您的choices数组为每个k值分配和初始化for循环。像for循环那样创建数组;

NSMutableArray *choices = [NSMutableArray new];

for (int k=0; k<dataQuestions.count; k++)
{
   ...
   ...

   [choices addObject:[[DBHelper getSharedInstance:[LoggedinUser sharedCenter].dbFileName] getQuestionChoicesByQuestionID:[[dataQuestions objectAtIndex:k] valueForKey:@"ids"]]];
  _singleSelectedData = [[choices valueForKey:@"choiceName"] objectAtIndex:k];
}

我在外面所以不能尝试代码,不能确保正常工作。测试一下,让我知道发生了什么事。