【问题标题】:UIPickerView not showing data correctly (Only Show "?")UIPickerView 未正确显示数据(仅显示“?”)
【发布时间】:2013-05-04 17:15:51
【问题描述】:

如果这个问题之前已经讨论过,请原谅我。
我在 UIPickerView 中显示数据时遇到问题,它只显示问号“?”

头文件.h

#import <UIKit/UIKit.h>
@interface CustomPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource> {
    NSMutableArray *arrayColors;
}

@property (strong, nonatomic) IBOutlet UIPickerView *kotaPicker;

@end

impl.m

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

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

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"The Data is %@", [arrayColors objectAtIndex:row]);
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    arrayColors = [[NSMutableArray alloc] init];
    [arrayColors addObject:@"Red"];
    [arrayColors addObject:@"Orange"];
    [arrayColors addObject:@"Yellow"];
    [arrayColors addObject:@"Green"];
    [arrayColors addObject:@"Blue"];
    [arrayColors addObject:@"Indigo"];
    [arrayColors addObject:@"Violet"];
    // Do any additional setup after loading the view from its nib.
}

【问题讨论】:

  • 您是否将 UIPickerView 委托设置为文件所有者

标签: ios objective-c uipickerview uipickerviewcontroller uipickerviewdatasource


【解决方案1】:

好的,你的问题解决了,问题是委托方法不正确,

您使用的方法(用于加载数据)不是委托方法,

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row

试试这个方法,(必须有效)

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

【讨论】:

  • 非常感谢..现在可以工作了..所以我忘记了在文档中复制所有方法名称。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
相关资源
最近更新 更多