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

标签 iphone ios objective-c nsmutablearray uipickerview

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

我知道语法应该是这样的:

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

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

-[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];
}

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

提前致谢

最佳答案

如果您的数组包含 Dictionar 或 NSMutableDictionar,那么您必须使用其键指定数组的值,如下所示尝试编写如下方法:

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

这是选取器 View 示例代码的基本实现:-

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

}

关于iphone - 如何让我的 UIPickerView 显示数组的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17760671/

相关文章:

iphone - 您可以在 iPhone 的文档文件夹中添加默认文档吗

ios - UITableView 拉取自动刷新

ios - UITabIeView CustomCell 在滚动之前无法正常工作... swift

ios - UIAlertController 之间的转换

iOS:启动由 AppDelegate 处理的自定义消息

iphone - 核心数据多线程

iPhone:如何以编程方式与 native 日历应用程序交互

带有文本字段的 iphone alertview

ios - ARKit 2 - 裁剪识别图像

ios - DOMException 105 仅在 UI 测试期间