在一个视图控制器中设置多个pickerView样式

时间:2013-11-18 09:55:34

标签: ios iphone uiview uilabel uipickerview

我是一名经验丰富的程序员,但对开发iOS应用程序不熟悉。

我有一个包含两个pickerView控制器的视图控制器,每个控制器解析一个不同的数组。像魅力一样工作,但我现在正试图按如下方式设计它们:

// Code to style pickerView, but viewForRow returns invalid row when using
// multiple instances of pickerView controller in one view controller
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *pickerLabel = (UILabel *)view;
    if (pickerLabel == nil) {
        //label size
        CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
        pickerLabel = [[UILabel alloc] initWithFrame: frame];
        [pickerLabel setBackgroundColor: [UIColor clearColor]];
        //here you can play with fonts
        [pickerLabel setFont: [UIFont fontWithName:@"Times New Roman" size:15.0]];
    }

    if (pickerView.tag == 1) {
        [pickerLabel setText: [self.arrayLeagues objectAtIndex:row]];
    } else {
        [pickerLabel setText: [self.arrayEvents objectAtIndex:row]];
    }

    return pickerLabel;
}

这会导致应用崩溃并导致以下错误:

-[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'

崩溃似乎是由于'row'变量(... viewForRow:(NSInteger)行...)在一个viewController中使用多个pickerView时没有重置造成的。

有关解决此问题的任何提示吗?

修改

我发现问题是由我的代码中的拼写错误引起的。下面的代码实际上应该按照它的方式工作。因此,如果您需要更新UIPickerView的Font或其他属性,请继续使用:)

0 个答案:

没有答案