将数据从数组添加到选择器组件

时间:2012-06-01 12:46:13

标签: iphone objective-c cocoa-touch ios4 nsarray

在我的应用程序中,我正在使用选择器控件,

我想从数组中获取数据到我的选择器视图

数组就像这样,

 PickerArray:(
    {
    CustEmail = test@test.com;
    CustFirstName = ABC;
    CustID = 1;
    CustLastName = Test;
    CustPhoneNo = 12345;
    }

我的选择器有一个组件,

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;

title = [[PickerArray objectAtIndex:row]valueForKey:@"CustFirstName"];
NSLog(@"title:%@",title);

return title;

}

有了这个我只得到一个值如果我想在一行选择器组件中获得CustFirstName和Email。 喜欢 ABC test@test.com;

请任何人建议我该怎么做?

3 个答案:

答案 0 :(得分:4)

您可以使用stringWithFormat:构建复合字符串,如下所示:

title = [NSString stringWithFormat:@"%@ %@"
,   [[PickerArray objectAtIndex:row]valueForKey:@"CustFirstName"]
,   [[PickerArray objectAtIndex:row]valueForKey:@"CustEmail"]
];

答案 1 :(得分:2)

根据要求撰写字符串:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{
  NSString *title = [NSString stringWithFormat:@"%@ %@",[[PickerArray objectAtIndex:row]valueForKey:@"CustFirstName"],[[PickerArray objectAtIndex:row]valueForKey:@"CustEmail"]];
 NSLog(@"title:%@",title);

 return title;
}

答案 2 :(得分:0)

尝试

title = [NSString stringWithFormat:@"%@ %@",[[PickerArray objectAtIndex:row]valueForKey:@"CustFirstName"],[[PickerArray objectAtIndex:row]valueForKey:@"CustEmail"]];