从UIPickerview中选择数据时,如何使用字符串格式显示UIlabel

时间:2014-03-10 15:01:19

标签: ios objective-c uilabel uipickerview

  

嗨,我有一个选择器视图。来自Web服务的这个选择器视图来了   加载数据。这个选择器视图是2部分。选择器视图1.部分是   项目名。和Picker视图2.部分是项目编号。打开时   屏幕项目没有标签是没有数据。当打开屏幕项目名称   标签没有数据。但是,当我旋转我的选择器视图来到这样的数据   ProjeAdı:“A1 Unitesi”Proje No:“002”。我怎么能当打开屏幕   来像旋转我的选择器这样的数据?

     

当打开屏幕项目名称和项目没有标签时没有数据。我想要   打开屏幕时显示数据

enter image description here

  

当我旋转选择器视图时,会出现这样的数据。

enter image description here

 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 2;
    }

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

        if (component == 0) {
            return [RaporlarList count];
        }
        return [RaporlarList count];

    }

    #pragma mark Picker Delegate Methods
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {

        if (component == 0) {
        eObje =  [RaporlarList objectAtIndex:row];
        return  eObje.ProjeAdii;

        }
        eObje = [RaporlarList objectAtIndex:row];
        return eObje.ProjeNoo;
    }

    #pragma mark -
    #pragma mark PickerView Delegate

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {

         if (component == 0)
{

    NSString *resultString = [[NSString alloc] initWithFormat:@"Proje Adı : %@", [[RaporlarList objectAtIndex:row] ProjeAdii]];
    lblProjeAdi.text = [NSString stringWithFormat:@"%@", resultString];

}
else
{
    NSString *resultString = [[NSString alloc] initWithFormat:@"Proje No: %@", [[RaporlarList objectAtIndex:row] ProjeNoo]];
    lblProjeNo.text = resultString;

}

       }

2 个答案:

答案 0 :(得分:1)

您为pickerview选择了错误的数据源。

您正在返回字符串,而是返回标签

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component
{

        // return your UILabel
}

答案 1 :(得分:0)

  

我的问题的正确答案如下:

  -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
        {

             if (component == 0)
    {

        NSString *resultString = [[NSString alloc] initWithFormat:@"Proje Adı : %@", [[RaporlarList objectAtIndex:row] ProjeAdii]];
        lblProjeAdi.text = [NSString stringWithFormat:@"%@", resultString];

    }
    else
    {
        NSString *resultString = [[NSString alloc] initWithFormat:@"Proje No: %@", [[RaporlarList objectAtIndex:row] ProjeNoo]];
        lblProjeNo.text = resultString;

    }

           }