UIPickerView不显示选择器中的数据,但显示在nslog中

时间:2014-01-16 11:01:30

标签: ios uipickerview uipickerviewdatasource

问题是选择器没有显示任何数据,但是数据存在于后台,因为我在nslog中滚动空的选择器视图,它显示数据。所以数据填充但不在uipickerview中显示。提前致谢

这是代码 在.h文件中我声明了

UIPickerView *picker;

.m文件

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];

类似于日期数组。

-(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;

}

3 个答案:

答案 0 :(得分:3)

问题是

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

如果删除它正常显示的部分。

希望它会对你有所帮助。

我也找到了根本原因。如果我得到更多信息,我会更新答案。

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

我找到了原因。 将UILabel init部件更改为

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

没关系。

答案 1 :(得分:2)

尝试这个....我认为你的选择器高度为零

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

    [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];

答案 2 :(得分:2)

试试这个

 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];
[picker reloadAllComponents];