iosuipickerviewuipickerviewdatasource

UIPickerView Not Displaying data in picker but displaying in nslog


The problem is that picker is not displaying any data but the data is present in the background as i scroll the empty pickerview in nslog it displays the data. So data is populating but not displaying in uipickerview. Thanks in advance

Here is the code in .h file i have declared

UIPickerView *picker;

.m file

picker = [
    [UIPickerView alloc] initWithFrame: CGRectMake(0, 100, 320, 0)];

[picker setDataSource: self];
[picker setDelegate: self];

picker.showsSelectionIndicator = YES;

peoplearray = [
    [NSArray alloc] initWithObjects: @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", nil];

timearray = [
    [NSArray alloc] initWithObjects: @"12:00 AM", @"12:30 AM", @"1:00 AM", @"1:30 AM", @"2:00 AM", @"2:30 AM", @"3:00 AM", @"3:30 AM", @"4:00 AM", @"4:30 AM", @"5:00 AM", @"5:30 AM", @"6:00 AM", @"6:30 AM", @"7:00 AM", @"7:30 AM", @"8:00 AM", @"8:30 AM", @"9:00 AM", @"9:30 AM", @"10:00 AM", @"10:30 AM", @"11:00 AM", @"11:30 AM", @"12:00 PM", @"12:30 PM", @"1:00 PM", @"1:30 PM", @"2:00 PM", @"2:30 PM", @"3:00 PM", @"3:30 PM", @"4:00 PM", @"4:30 PM", @"5:00 PM", @"5:30 PM", @"6:00 PM", @"6:30 PM", @"7:00 PM", @"7:30 PM", @"8:00 PM", @"8:30 PM", @"9:00 PM", @"9:30 PM", @"10:00 PM", @"10:30 PM", @"11:00 PM", @"11:30 PM", nil];

similar is the date array.

-(NSInteger) numberOfComponentsInPickerView: (UIPickerView * ) pickerView {
    return 4;
}



-(NSInteger) pickerView: (UIPickerView * ) pickerView numberOfRowsInComponent: (NSInteger) component {

    if (component == 0) {
        return [peoplearray count];
    } else if (component == 1) {
        return [datearray count];
    } else if (component == 2)

    {
        return [timearray count];
    } else if (component == 3)

    {
        return [timearray count];
    }

    return 0;
}

-(NSString * ) pickerView: (UIPickerView * ) pickerView titleForRow: (NSInteger) row forComponent: (NSInteger) component {

    if (component == 0)

    {
        return [peoplearray objectAtIndex: row];
    } else if (component == 1)

    {
        return [datearray objectAtIndex: row];
    } else if (component == 2)

    {
        return [timearray objectAtIndex: row];
    } else if (component == 3)

    {

        return [timearray objectAtIndex: row];
    }

    return 0;

}

- (CGFloat) pickerView: (UIPickerView * ) pickerView widthForComponent: (NSInteger) component {
    switch (component) {

        case 0:

            return 35;

        case 1:

            return 100;

        case 2:

            return 80;

        case 3:

            return 80;
    }

    return 0;
}

- (UIView * ) pickerView: (UIPickerView * ) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (UIView * ) view {

    UILabel * label = [
        [UILabel alloc] initWithFrame: CGRectMake(0, 0, 300, 37)];

    if (component == 0)

    {
        label.text = [peoplearray objectAtIndex: row];

    } else if (component == 1)

    {
        label.text = [datearray objectAtIndex: row];
    } else if (component == 2)

    {
        label.text = [timearray objectAtIndex: row];

    } else if (component == 3)

    {
        label.text = [timearray objectAtIndex: row];
    }

    label.backgroundColor = [UIColor clearColor];

    return label;

}

Solution

  • The problem is

    - (UIView * ) pickerView: (UIPickerView * ) pickerView viewForRow: (NSInteger) row forComponent: (NSInteger) component reusingView: (UIView * ) view 
    

    If you delete that part it shows normally.

    Hopes it will help you a little.

    I'm finding the root reason also.If I got more info, I will update the answer.

    /////////////////

    I found the reason. Change the UILabel init part to

    UILabel *label = [[UILabel alloc] init];
    

    It will be OK.