Search code examples
iphoneiosnsarrayuipickerview

UIPickerView wont display data properly


I am trying to make a UIPickerView with data @"0", @"1", @"2", @"3"... display on rows, it will be a 2 columns pickerView.

here is my code:

this is .h file

#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

this is .m file

#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

My problem is if I try to run the app, the UIPickerView isn't filled with any data... so absolutely empty.... and if i try to scull the 'selected indicator', app will crash at once... this is the error msg -> Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableViewRowData.m:1630

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path ( 2 indexes [0, 0])'

any suggestions ? thanks


Solution

  • It looks like you are not creating your UIPickerView in code. Did you make sure that the delegate and data source of the picker is the class you are trying to create it in? If it is not, it will not know what you want to have in it.