如何让我的UIPickerView显示数组的所有元素

时间:2013-07-20 09:15:20

标签: iphone ios objective-c nsmutablearray uipickerview

我有一个UIPickerView和一个数组,虽然我似乎无法将数组中的所有日期输入到UIPicker。

我知道语法是这样的:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   return [treatments objectAtIndex:row];
}

虽然处理是数组名称,但是当我使用它时会出现这个错误:

-[Treatment isEqualToString:]: unrecognized

我搜索了整个项目,找不到短语:isEqualToString

Treatment.h文件:

#import <Foundation/Foundation.h>

@interface Treatment : NSObject {
    NSString *treatmentid;
    NSString *treatmentName;
    NSString *treatmentPrice;
}

@property (nonatomic, strong) NSString *treatmentid;
@property (nonatomic, strong) NSString *treatmentName;
@property (nonatomic, strong) NSString *treatmentPrice;

@end

所有选择码:

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [treatments count];
}

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [treatments objectAtIndex:row];
}

如果您需要更多代码,请说

提前致谢

1 个答案:

答案 0 :(得分:1)

你的数组包含Dictionar或NSMutableDictionar然后你必须用它的键指定数组的值,如下所示尝试写这样的方法:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   return[[treatments objectAtIndex:row]valueForKey:@"YourKey"];
}

以下是Picker视图示例代码的基本实现: -

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thepickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thepickerView numberOfRowsInComponent:(NSInteger)component
{
        return [treatments count];
}

自定义UIpickerview的显示标签,如下: -

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];



        if (component == 0) {

            label.font=[UIFont boldSystemFontOfSize:22];
            label.textAlignment = UITextAlignmentCenter;
            label.backgroundColor = [UIColor clearColor];



            label.text = [NSString stringWithFormat:@"%d", row];
            label.font=[UIFont boldSystemFontOfSize:22];

             NSLog(@"%@",[yourpickerview selectedRowInComponent:component]);

        }

    return label;

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

   NSLog(@"%@",[treatments objectAtIndex:row]);

}