0

我正在尝试使用数据 @"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 没有填充任何数据......所以绝对是空的......如果我尝试 scull '选定的指标',应用程序将立即崩溃......这是错误消息 -> -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630 中的断言失败

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

有什么建议么 ?谢谢

4

1 回答 1

3

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

于 2013-03-23T23:18:25.540 回答