0

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

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

我的两个问题是:

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

2) With all the components and arrays in my UIPickerView, the only way I was able to get my data from the arrays to properly display when a certain combination of pickerwheels is selected was to write A LOT of "if" and "else 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]];
    }
4

1 回答 1

0

为什么不以某种方式优化代码是这样的:

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

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

这干净多了:)

于 2010-01-08T20:24:19.360 回答