简体   繁体   English

iOS表达式结果未在viewDidLoad方法中使用

[英]iOS Expression result unused in viewDidLoad method

I'm working on an iOS app and need to display a PickerView containing two components, one with strings and one with UIImageViews. 我正在使用iOS应用程序,需要显示一个PickerView,其中包含两个组件,一个包含字符串,一个包含UIImageViews。 In the view controller's class extension, I've created a NSArray called "suites" to hold the UIImages, and populate the array in the viewDidLoad method: 在视图控制器的类扩展中,我创建了一个名为“ suites”的NSArray来保存UIImages,并在viewDidLoad方法中填充该数组:

viewDidLoad method viewDidLoad方法

I've also assigned the view controller to be the PickerView's delegate and data source. 我还已将视图控制器分配为PickerView的委托和数据源。 I have to global variables to identify PickerView's two components: 我必须全局变量来识别PickerView的两个组件:

#define numberComponent 0
#define suiteComponent 1

and have implemented the following delegate method: 并实现了以下委托方法:

- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
            forComponent:(NSInteger)component
            reusingView:(nullable UIView *)view
{
    if (component == suiteComponent){
        UIImageView *imageView = (UIImageView *) view;
        if(!imageView){
            imageView = [[UIImageView alloc] initWithImage: self.suites[row]];
        }
        imageView.image = self.suites[row];
        return imageView;
    }
    else{
        UILabel *label = (UILabel *) view;
        if(!label){
            label = [[UILabel alloc] init];
        }
        label.text = self.numbers[row];
        return label;
    }
}

In the simulator, the picker component containing the UILabels is rendering just fine. 在模拟器中,包含UILabels的选择器组件可以很好地呈现。 The component containing the UIImageViews will not show up, and I am guessing it is because the array of UIImages is not being used: 包含UIImageViews的组件将不会显示,我猜测是因为未使用UIImages数组:

simulator 模拟器

Update: The block within if(component == suiteComponent) is never reached. 更新:永远不会到达if(component == suiteComponent)中的块。 I have this datasource method: 我有此数据源方法:

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

so if my understanding is correct, UIPickerView should eventually make a call to its delegate to render the second component. 因此,如果我的理解是正确的,则UIPickerView最终应调用其委托以呈现第二个组件。 However when I debug it, the delegate method's row parameter stays between 0 and 4 throughout all of the calls, and the component variable stays at 0. 但是,当我调试它时,委托方法的行参数在所有调用中都保持在0到4之间,而组件变量保持在0上。

I'd appreciate any help, thank you! 我将不胜感激,谢谢!

That is incorrect way to initiate a custom object. 这是启动自定义对象的错误方法。

You should do it like this: 您应该这样做:

self.suites = [[Suites alloc] initWithObjects:@[heart,club,spade,diamond]];

then u can use self.suites properly. 那么您可以正确使用self.suites。 Note I assumed your custom object class name is "Suites". 注意我假设您的自定义对象类名称是“套房”。 You need to change that name to whatever your custom class name is. 您需要将该名称更改为您的自定义类名称。 I also assume the function accepts NSArray, thus I create the literal ObjC array @[..]. 我还假设该函数接受NSArray,因此我创建了文字ObjC数组@ [..]。

And the parameters passing is suspicious too. 并且参数传递也很可疑。 You don't need to cast the object types in parameters passing. 您不需要在参数传递中强制转换对象类型。 You need to check the custom class's initWithObjects: declaration. 您需要检查自定义类的initWithObjects:声明。 Is it declared to input arrays or UIImage? 是否声明为输入数组或UIImage?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM