无法向选择器视图添加更多组件

时间:2014-08-15 11:59:00

标签: objective-c uipickerview

我非常确定我正确使用了这种方法:

-(NSInteger) numberOfComponentsinPickerView:(UIPickerView *)pickerView {
    return 3;
}

不足以在选择器中创建三个组件,因为当我运行程序时,它只显示我原来拥有的一个组件。

这里是我使用的代码:

    // Create the picker
-(void) createPickerData {

    //Set up a path to the plist
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *finalPath = [path stringByAppendingPathComponent:@"WheelData.plist"];
    NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:finalPath];

    NSArray *letterArray = [NSArray arrayWithArray:[plistData objectForKey:@"LetterArray"]];
    numberOfLetters = [letterArray count];


    NSMutableArray *firstLetterArray = [[NSMutableArray alloc] initWithCapacity:numberOfLetters];

    for (int i = 0; i < numberOfLetters; i++) {
        [firstLetterArray addObject:[[letterArray objectAtIndex:i] objectForKey:@"Letter"]];
    }

    pickerDataForName = [NSArray arrayWithArray:firstLetterArray];


}

// Positions the picker on the screen
-(void) showPicker {

    CGRect rect =CGRectMake(60, 650, self.frame.size.width - 120, 400);

    enterNamePicker = [[UIPickerView alloc] initWithFrame:rect];
    enterNamePicker.delegate = self;

    [self addSubview:enterNamePicker];

}

//This sets the number of components wheels (or columns) in a picker view
-(NSInteger) numberOfComponentsinPickerView:(UIPickerView *)pickerView {

    //Creating 3 wheels
    return 3;
}

// Defines the how many rows (or items) are in the picker view
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

    return [pickerDataForName count];

}

// is run anytime you change the picker
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:   (NSInteger)component reusingView:(UIView *)view {



    //Here we are seting up a label for each object in each row / component
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor whiteColor];
    //label.adjustsFontSizeToFitWidth = YES;
    label.textColor = [UIColor blackColor];
    label.font = [UIFont fontWithName:universalFont size:30];
    label.text = [NSString stringWithFormat:@"%@", [pickerDataForName objectAtIndex:row]];


    return label;
}

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    NSLog(@"Row %i selected", [enterNamePicker selectedRowInComponent:0]);

}

0 个答案:

没有答案