【问题标题】:Programmatically add uipickerview and its data以编程方式添加 uipickerview 及其数据
【发布时间】:2014-07-22 13:26:18
【问题描述】:

在我看来确实加载了一个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 因为我只有 1 个数组

- (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 都必须显示自己的数据。任何建议将不胜感激

【问题讨论】:

  • 显示- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 的代码
  • 您的代码一次又一次地创建每个对象。在 viewDidLoad 方法中分配您的 uielements 并尝试在 viewWillAppear 中读取 sqlite 数据并将其分配给数组。
  • 我通过添加以下代码来编辑我的答案: - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

标签: ios objective-c uipickerview


【解决方案1】:

您的 choices 数组在 for 循环中为每个 k 值分配和初始化。像这样从 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];
}

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

【讨论】:

  • 感谢您的回答,但这不是问题所在。问题是有 pnly _singleSelectedData 数组来填充 uipickerview 并且它加载到 viewDidLoad 所以我尝试将它加载到按钮的发件人中。我还创建了一个字典来加载其中的所有数组。值是我的选择数组,键是 questionID。然后在按钮的发件人(按钮的发件人标签也等于 questionID)我重新加载 uipickerview。所以我现在就这样解决了。但我仍然在努力改进它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多