0

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

我知道语法是这样的:

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

治疗方法是数组名称,但是当我使用它时会出现以下错误:

-[Treatment isEqualToString:]: unrecognized

我搜索了我的整个项目,但找不到这句话:isEqualToString

治疗.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];
}

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

提前致谢

4

1 回答 1

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]);

}
于 2013-07-20T09:18:52.463 回答