【问题标题】:How to make my UIPickerView display all elements of an array如何让我的 UIPickerView 显示数组的所有元素
【发布时间】:2013-07-20 09:15:20
【问题描述】:

我有一个 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];
}

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

提前致谢

【问题讨论】:

  • 删除您的应用。从模拟器中清理您的项目,然后再次运行。
  • 我该怎么做???
  • 我对这些东西很陌生,所以放轻松
  • 在您的模拟器中,按住应用程序的图标并删除您的应用程序。并转到 xcode 项目和“产品”菜单并选择“清洁”.. 并运行您的应用程序
  • 嗯。 @iPatel 还是同样的问题

标签: iphone ios objective-c nsmutablearray uipickerview


【解决方案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]);

}

【讨论】:

  • 你介意我再问你一个刚刚出现的问题
  • 说我想给你解决方案
猜你喜欢
  • 1970-01-01
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2014-07-29
  • 1970-01-01
相关资源
最近更新 更多