Javascript 加速计+;合金&x2B;通过对象循环求值-$将其打断

Javascript 加速计+;合金&x2B;通过对象循环求值-$将其打断,javascript,loops,runtime-error,appcelerator,titanium-alloy,Javascript,Loops,Runtime Error,Appcelerator,Titanium Alloy,我在屏幕上有384个对象,我需要按照特定的顺序进行迭代,根据不断变化的变量更改它们的属性。以下是.xml文件中对象的外观: <View formFactor="tablet" id="res1_1" class="resBit" left="1.5%" bottom="25.0%" /> <View formFactor="tablet" id="res1_2" class="resBit" left="1.5%" bottom="27.3%" />

我在屏幕上有384个对象,我需要按照特定的顺序进行迭代,根据不断变化的变量更改它们的属性。以下是.xml文件中对象的外观:

    <View formFactor="tablet" id="res1_1" class="resBit" left="1.5%" bottom="25.0%" />
    <View formFactor="tablet" id="res1_2" class="resBit" left="1.5%" bottom="27.3%" />
    <View formFactor="tablet" id="res1_3" class="resBit" left="1.5%" bottom="29.6%" />
    <View formFactor="tablet" id="res1_4" class="resBit" left="1.5%" bottom="31.9%" />
    [...]
    <View formFactor="tablet" id="res16_22" class="resBit" left="93.0%" bottom="73.3%" />
    <View formFactor="tablet" id="res16_23" class="resBit" left="93.0%" bottom="75.6%" />
    <View formFactor="tablet" id="res16_24" class="resBit" left="93.0%" bottom="77.9%" />

如果我只写
$.res1\u 1.setOpacity(1)直接在代码中运行。是评估破坏了它。思想?谢谢。

除了那个错误,还有很多事情需要注意

第一件事是彻底检查您的UI结构。当前,如果您确实可以使用布局类型正确创建视图,则不建议手动设置左/下/上/右属性

从XML代码的外观来看,似乎您正在创建一个包含16列x 25行的网格视图,每个视图的左填充为1.5%,顶部填充为2.3%(这里您将其作为底部)您当前如何设计它的主要问题是手动使用左/下属性当然为什么有人会手动创建384个视图:)。。这样做需要极大的耐心&至少我没有:)

现在,创建此类UI的推荐方法如下所示:

template.xml

<Alloy>
    <View class="resBit" left="1.5%" top="2.3%" />
</Alloy>
<View id='parentContainer' formFactor="tablet" layout='horizontal' horizontalWrap='true' right='1.5%'>
    ..... here comes your those resBit views created dynamically
    // formFactor property is only set to parent container which also applies to its child container, so no need to set it on all 384 child views as you did

    // horizontalWrap property ensures that child-views which are going out of bound, will be created in
    // next row & thus it creates a grid-view automatically while using proper sizes so the last or
    // 16th view fits to the end of parent view.
</View>
最后,因为您没有res12\u 14等ID。您仍然可以访问所有子视图,如下所示:

你的_view.js

// create child-views dynamically like this
for (var i=1; i<=384; i++) {
    // you can pass any required arguments here needed to create child-views
    var templateView = Alloy.createController('template', {
        position : i
    }).getView();

    $.parentContainer.add(templateView);
}
var pos = $.args.position;

// if it's the last or 16th child-view in each row, then set its width to fill the parent width, which will make sure that your next child-view will move to a new row.
// it's necessary because sometimes while using %age dimensions, there might be a few pixels calculation error which can create the 16th child-view to next row

if ((pos % 16) == 0) {
    $.template.width = Ti.UI.FILL;
}
// theValues rotates between a set of 100 or so such combinations
theValues = "2,2,3,4,5,5,4,3,2,2,3,4,5,5,4,3".split(",");

// gives you all child-views in an array
var child_views = $.parentContainer.children;

// Start on the left and move right
for (i=1; i<17; i++) {

    // Start at the bottom and move up
    for (ii=1; ii<25; ii++) {
        if (ii < (theValues[i-1]) - 1) {
            var currentChildPos = i * ii - 1;       // since we started loop from 1 & array index starts from 0
            // Make first row solid
            if (i == 1) {
                child_views[currentChildPos].setOpacity(1);
            }

            // Paint reds
            child_views[currentChildPos].setBackgroundColor("red");
        }
    }
}
//这些值在大约100个这样的组合之间旋转
值=“2,2,3,4,5,5,4,3,2,2,3,4,5,5,4,3”。拆分(“,”;
//提供数组中的所有子视图
var child_views=$.parentContainer.children;
//从左边开始,然后向右移动

对于(i=1;我已经有一段时间没有使用appcelerator了,但我认为如果(i==1){$['res'+i+'+'.+ii].setOpacity(1);}
,你可以这样做
。谢谢你的洞察力。
var pos = $.args.position;

// if it's the last or 16th child-view in each row, then set its width to fill the parent width, which will make sure that your next child-view will move to a new row.
// it's necessary because sometimes while using %age dimensions, there might be a few pixels calculation error which can create the 16th child-view to next row

if ((pos % 16) == 0) {
    $.template.width = Ti.UI.FILL;
}
// theValues rotates between a set of 100 or so such combinations
theValues = "2,2,3,4,5,5,4,3,2,2,3,4,5,5,4,3".split(",");

// gives you all child-views in an array
var child_views = $.parentContainer.children;

// Start on the left and move right
for (i=1; i<17; i++) {

    // Start at the bottom and move up
    for (ii=1; ii<25; ii++) {
        if (ii < (theValues[i-1]) - 1) {
            var currentChildPos = i * ii - 1;       // since we started loop from 1 & array index starts from 0
            // Make first row solid
            if (i == 1) {
                child_views[currentChildPos].setOpacity(1);
            }

            // Paint reds
            child_views[currentChildPos].setBackgroundColor("red");
        }
    }
}