如何让UIPickerView显示其数据

时间:2010-01-08 17:16:02

标签: multidimensional-array uipickerview

我是iPhone和Objective C编程的新手,但我能够使我的代码工作。我只是不确定这是否是最好的方法,因为它需要很多代码并且看起来不漂亮。

我有一个UIPickerView滚轮,它有5个组件,120个多维数组(每个包含13行和7列),我放在“DidSelectRow”方法中。

我的两个问题是:

1)我是不是应该将所有这些数组放在方法中,而是学习将它们放在一个单独的文件中,也许是一个SQL数据库?就像我说的那样,pickerview工作正常,它只是不是很好的编码,也许它不会让我的应用尽可能高效地运行。

2)使用UIPickerView中的所有组件和数组,当选择某个选择轮组合时,我能够从数组中获取数据以正确显示的唯一方法是编写大量“if”和“否则如果“陈述。它看起来很荒谬,必须有更好的方法!我无法弄清楚我是否可以使用或如何为我的方法使用Switch语句。我已经提供了一些示例代码,如果这有助于任何人了解我正在尝试做什么。

提前感谢您的帮助!

if (pressAltWheel == 0 && antiIceWheel == 0 && thrustWheel == 0)
    {
        {
            //4600ft
            if (tempWheel == 9 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[0][0]];

        else if (tempWheel == 11 && runwayLengthWheel == 0)
                weightValue.text = [NSString stringWithFormat:@"%@",column0[1][0]];

        else if (tempWheel == 13 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[2][0]];

        else if (tempWheel == 15 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[3][0]];

        else if (tempWheel == 16 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[4][0]];

        else if (tempWheel == 17 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[5][0]];

        else if (tempWheel == 18 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[6][0]];

        else if (tempWheel == 19 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[7][0]];

        else if (tempWheel == 20 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[8][0]];

        else if (tempWheel == 21 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[9][0]];

        else if (tempWheel == 22 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[10][0]];

        else if (tempWheel == 23 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[11][0]];

        else if (tempWheel == 24 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[12][0]];
        }

        //5300ft
        {


        if (tempWheel == 9 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[0][1]];

        else if (tempWheel == 11 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[1][1]];

        else if (tempWheel == 13 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[2][1]];

        else if (tempWheel == 15 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[3][1]];

        else if (tempWheel == 16 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[4][1]];

        else if (tempWheel == 17 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[5][1]];

        else if (tempWheel == 18 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[6][1]];

        else if (tempWheel == 19 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[7][1]];

        else if (tempWheel == 20 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[8][1]];

        else if (tempWheel == 21 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[9][1]];

        else if (tempWheel == 22 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[10][1]];

        else if (tempWheel == 23 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[11][1]];

        else if (tempWheel == 24 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[12][1]];
    }

1 个答案:

答案 0 :(得分:0)

为什么不以某种方式优化代码:

if (tempWheel >= 15)
    weightValue.text = [NSString stringWithFormat:@"%@",column0[tempWheel-12][runwayLengthWheel]];

else
    weightValue.text = [NSString stringWithFormat:@"%@",column0[(tempWheel-9)/2][runwayLengthWheel]];

这个更清洁:)