UIPickerView不会正确显示数据

时间:2013-03-23 23:08:16

标签: iphone ios nsarray uipickerview

我正在尝试使用数据@“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

这是.m文件

#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

我的问题是,如果我尝试运行应用程序,UIPickerView没有填充任何数据...所以绝对空... 如果我试图剔除“选定的指标”,应用程序将立即崩溃... 这是错误消息 - &gt; 断言失败 - [UITableViewRowData rectForRow:inSection:],/ SourceCache / UIKit_Sim / UIKit-2380.17 / UITableViewRowData.m:1630

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

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:3)

看起来您没有在代码中创建UIPickerView。您是否确保选择器的委托和数据源是您尝试创建它的类?如果不是,它就不会知道你想要的东西。