iphone - UIPickerView 无法正确显示数据

标签 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 没有填充任何数据...所以完全是空的... 如果我尝试划掉“选定指标”,应用程序将立即崩溃...... 这是错误信息 -> -[UITableViewRowData rectForRow:inSection:],/SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630 断言失败

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

有什么建议吗?谢谢

最佳答案

看起来您没有在代码中创建 UIPickerView。您确定选择器的委托(delegate)和数据源是您尝试在其中创建它的类吗?如果不是,它就不知道你想要什么。

关于iphone - UIPickerView 无法正确显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15593145/

相关文章:

iphone - 如何从 Xcode 项目中删除 iPhone 5 兼容性?

c# - 在 native iOS/Android 应用程序中使用 Xamarin 库

iphone - NSArray 给出 NSZombie 错误

ios - 使用 NSArray 值高效创建占位符模板 NSString

iphone - 什么时候使用单元测试?

iphone - 通过应用内购买将现有的付费应用程序转换为免费版本

iphone - 录制视频并同时播放视频

ios - 从 [PFObject] 向下转换为 [PFObject]

iOS MKLocalSearchRequest 排除企业

iphone - 搜索 NSArray 包含 NSDictionary,值为 "time efficiency"