Iphone UIPickerView无法正确显示数据

Iphone UIPickerView无法正确显示数据,iphone,ios,nsarray,uipickerview,Iphone,Ios,Nsarray,Uipickerview,我正在尝试使用数据@“0”@“1”@“2”@“3”创建UIPickerView。。。按行显示, 它将是一个2列pickerView 这是我的密码: 这是一个.h文件 #import <UIKit/UIKit.h> @interface CustomNumberViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> @property (strong, nonato

我正在尝试使用数据@“0”@“1”@“2”@“3”创建UIPickerView。。。按行显示, 它将是一个2列pickerView

这是我的密码:

这是一个.h文件

#import <UIKit/UIKit.h>

@interface CustomNumberViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet UIPickerView *numberPicker;
@property(strong, nonatomic)NSArray *listOfNumbers;
@property (strong,nonatomic)NSString *numberOfFirstColumn;
@property(strong,nonatomic)NSString *numberOfSecondColumn;

@end
我的问题是,如果我尝试运行应用程序,UIPickerView中不会填充任何数据。。。所以绝对是空的。。。。 如果我试图划船的'选定指标',应用程序将立即崩溃。。。 这是错误消息-> -[UITableViewRowData rectForRow:inSection:]、/SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630中的断言失败

由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:“请求在无效索引路径(2个索引[0,0])处执行rect”


有什么建议吗?谢谢

看起来您没有在代码中创建UIPickerView。是否确保选取器的委托和数据源是您试图在其中创建它的类?如果不是,它将不知道您想要在其中包含什么。

看起来您没有在代码中创建UIPickerView。是否确保选取器的委托和数据源是您试图在其中创建它的类?如果不是,它将不知道您想要在其中包含什么。

我按ctrl键并将选择器从情节提要拖动到UIPickerView*numberPicker,在.m中,我还按self.numberPicker.ShowsSelectIndicator=TRUE@塞普,那不是他问的。问题是,您是否将数据源和选择器的委托设置为控制器类?如果在IB中的选择器上控制单击(或右键单击),它将显示黑色窗口。您应该在那里看到数据源和委托。它们都需要连接到您的控制器。酷,只需将委托设置为控制器类,它现在就可以工作了,但只显示一列数字…@seph,因为这是您告诉它的。您将为numberOfComponents返回1。我按ctrl键并将选择器从情节提要拖动到UIPickerView*numberPicker,在.m中我还按self.numberPicker.ShowsSelectIndicator=TRUE@塞普,那不是他问的。问题是,您是否将数据源和选择器的委托设置为控制器类?如果在IB中的选择器上控制单击(或右键单击),它将显示黑色窗口。您应该在那里看到数据源和委托。它们都需要连接到您的控制器。酷,只需将委托设置为控制器类,它现在就可以工作了,但只显示一列数字…@seph,因为这是您告诉它的。对于numberOfComponents,返回1。
#import "CustomNumberViewController.h"

@interface CustomNumberViewController ()

@end

@implementation CustomNumberViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    //UIView background
    UIGraphicsBeginImageContext(self.view.frame.size);
    [[UIImage imageNamed:@"pickerbackground.png"] drawInRect:self.view.bounds];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.view.backgroundColor = [UIColor colorWithPatternImage:image];

    //init picker
    self.numberPicker.showsSelectionIndicator = TRUE;
    self.listOfNumbers = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",nil];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//Number of rows to display in each component
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==0) {
        return [self.listOfNumbers count];
    }
    return [self.listOfNumbers count];
}

//Number of columns to display
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//define what to display in each rows and columns
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==0) {
        return [self.listOfNumbers objectAtIndex:row];
    }
    return [self.listOfNumbers objectAtIndex:row];
}

//selected number to be stored in nsstring
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component==0) {
        self.numberOfFirstColumn = [self.listOfNumbers objectAtIndex:row];

    }
    self.numberOfSecondColumn = [self.listOfNumbers objectAtIndex:row];
}

@end