akkyie / AKPickerView

A simple yet customizable horizontal picker view.
MIT License
894 stars 106 forks source link

Crashes when datasource array is empty. #51

Closed RishabhTayal closed 9 years ago

RishabhTayal commented 9 years ago

It crashes on line #188 when number of items in datasource array is 0.

NSAssert(item < [self.collectionView numberOfItemsInSection:0],
         @"item out of range; '%lu' passed, but the maximum is '%lu'", item, [self.collectionView numberOfItemsInSection:0]);
akkyie commented 9 years ago

Did you confirm that the count of items in your array is greater than the number returned from numberOfItemsInPickerView?

RishabhTayal commented 9 years ago

What I did is, I deleted all the items in the array in your demo project. This makes it an empty array.

RishabhTayal commented 9 years ago

Ideally it should accept an empty array as datasource array in - (NSUInteger)numberOfItemsInPickerView:(AKPickerView *)pickerView

Here is the code from your test project modified:

#import "AKViewController.h"

#import "AKPickerView.h"

@interface AKViewController () <AKPickerViewDataSource, AKPickerViewDelegate>
@property (nonatomic, strong) AKPickerView *pickerView;
@property (nonatomic, strong) NSArray *titles;
@end

@implementation AKViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pickerView = [[AKPickerView alloc] initWithFrame:self.view.bounds];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.pickerView];

    self.pickerView.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
    self.pickerView.highlightedFont = [UIFont fontWithName:@"HelveticaNeue" size:20];
    self.pickerView.interitemSpacing = 20.0;
    self.pickerView.fisheyeFactor = 0.001;
    self.pickerView.pickerViewStyle = AKPickerViewStyle3D;
    self.pickerView.maskDisabled = false;

    self.titles = @[];

    [self.pickerView reloadData];
}

#pragma mark - AKPickerViewDataSource

- (NSUInteger)numberOfItemsInPickerView:(AKPickerView *)pickerView
{
    return [self.titles count];
}

- (NSString *)pickerView:(AKPickerView *)pickerView titleForItem:(NSInteger)item
{
    return self.titles[item];
}

#pragma mark - AKPickerViewDelegate

- (void)pickerView:(AKPickerView *)pickerView didSelectItem:(NSInteger)item
{
    NSLog(@"%@", self.titles[item]);
}

@end
akkyie commented 9 years ago

I got it, it is a bug. I'm fixing it.

RishabhTayal commented 9 years ago

If you need, I would love to help you!

akkyie commented 9 years ago

Thank you, but the work is done (b268b2972b1ec6a675d6bd8b04d7ca83b5fb9b58.) Again thanks a lot for your reporting!

RishabhTayal commented 9 years ago

@Akkyie You are welcome. Always happy to help.