0

我正在从 Web 服务加载一个数组并在选择器中显示它们,一切正常。我的数组包含每个问题类别的随机问题。现在我想要的是我想在数组末尾再添加一个条目“您自己的问题”,以便选择器的最后一行显示“您自己的问题”。我怎样才能做到这一点

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
    }

   // retval.text = [idAndNameArray objectAtIndex:row];
    if([pickerType isEqualToString:@"picker"])
    {
        retval.text = [idAndNameArray objectAtIndex:row];
    }
    else
    {
        retval.text = [quesArray objectAtIndex:row];
    }
    retval.font = [UIFont boldSystemFontOfSize:14];
    retval.numberOfLines = 3;
    return retval;
}

此代码仅显示从 Web 服务获取的行数(问题)。

编辑1:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{

    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
    if([pickerType isEqualToString:@"picker"])
    {
        return [idAndNameArray count];
    }
    else
    {
       return [quesArray count];
    }

}

编辑2:

通过使用我的自定义对象“您自己的问题”创建另一个 NSMutableArray 并使用从 Web 服务获取的数组附加它来解决

NSMutableArray *customObj = [NSMutableArray arrayWithObject: @"test"];
 [quesArray addObjectsFromArray:customObj];
4

1 回答 1

1

UIPickerViewDataSource称为pickerView:numberOfRowsInComponent:返回数组计数加一个值(return arr.count+1;)的方法中,在您实现的方法中,当大于( )UIPickerVIewDelegate时,返回带有文本的标签。祝你好运!componentarr.count-1if(row>(arr-1)){//return the label with 'Your own question'}

编辑:

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
    if([pickerType isEqualToString:@"picker"])
    {
        return [idAndNameArray count];
    }
    else
    {
       return [quesArray count]+1;
    }

}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *retval = (id)view;
    if (!retval) {
        retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
    }

   // retval.text = [idAndNameArray objectAtIndex:row];
    if([pickerType isEqualToString:@"picker"])
    {
        retval.text = [idAndNameArray objectAtIndex:row];
    }
    else
    {
       if(row>quesArray.count-1){
         retval.text = @"You own question";
       }
       else{
            retval.text = [quesArray objectAtIndex:row];
         }
        }
        retval.font = [UIFont boldSystemFontOfSize:14];
        retval.numberOfLines = 3;
        return retval;
    }
于 2013-04-16T10:36:45.520 回答