UIPickerview问题

时间:2013-05-19 20:03:52

标签: objective-c uipickerview

好的让我再试一次,我的应用程序中有一个选择器,这里的代码工作正常

_fuglar = [[NSArray alloc]initWithObjects:@"Allt árið", @"Hrafn", @"Svartbakur",@"Silfurmáfur",@"Sílamáfur",@"Frá 20.ágúst til 15.mars",@"Grágæs",@"Heiðagæs",@"Frá 1.september til 15.mars",@"Fýll",@"Dílaskarfur", nil];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - UIPickerView Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _fuglar.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_fuglar objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Create UIImage objects
UIImage * uglaImage = [UIImage imageNamed:@"ugla1.jpg"];
UIImage * ravenImage = [UIImage imageNamed:@"raven.jpg"];
UIImage * svartbakurImage = [UIImage imageNamed:@"svartbakur2.jpg"];

然后我想把文本放在选择器中并改变颜色,我正在使用这个代码然后我的选择器只是空白但我可以看到图片,任何想法?

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blueColor];
return label;

1 个答案:

答案 0 :(得分:2)

填充选择器视图时,可以实现titleForRow或viewForRow,而不是两者。通过实现viewForRow,您将视图替换为默认标签。要解决此问题,请删除titleForRow方法,并在viewForRow中设置标签文本:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    if (row == 0 || row == 2) {
        label.textColor = [UIColor blueColor];
    }else{
        label.textColor = [UIColor blackColor];
    }
    label.text = [_fuglar objectAtIndex:row];
    return label;
}