Titanium for循环只执行一次

时间:2014-01-08 00:03:06

标签: titanium

我正在尝试将数据从数组传递到新的View对象中,虽然'accounts.length'返回6,但循环只执行一次。

var title;
var id;
var button;
var v;
//alert(accounts.length); --> 6
for(i=0; i<accounts.length; i++) {

    title = accounts[i].title;
    id = accounts[i].id;
    button = Titanium.UI.createButton({
        backgroundImage: 'images/List.png',
        height:37,
        width: 37,
    });
    menuBtns.push(button);
    v = new HomeView({menuBtn:button, id:id});
    //alert(title'); --> only once
    data.push({ 
        title:title,
        view:v,
        dataType:'dashboardapplication'
    });
}

Accounts是一个简单的多维数组,HomeView是一个简单View上的层,data是一个已经包含数据的数组(与push语句的格式相同)。 有人遇到过同样的问题吗?

1 个答案:

答案 0 :(得分:0)

您显示的代码中没有任何内容可以阻止循环继续。如果我在你的代码中推断出缺少的变量和函数,我得到类似下面的东西,它成功运行并通过6个循环。因此,代码中的其他地方必定存在问题。尝试在try {} catch (e) { alert(e); }中包装for循环。尝试提出一个更简单的例子来重现你所看到的bug。尝试删除代码中的一些内容,并查看阻止其完成的内容。

var accounts = [
        { title: '1', id: '1' },
        { title: '2', id: '2' },
        { title: '3', id: '3' },
        { title: '4', id: '4' },
        { title: '5', id: '5' },
        { title: '6', id: '6' }
    ],
    menuBtns = [],
    data = [];

function HomeView(args) {
}

var title;
var id;
var button;
var v;
alert('accounts length: ' + accounts.length); --> 6
for (i = 0; i < accounts.length; i++) {
    title = accounts[i].title;
    id = accounts[i].id;
    button = Titanium.UI.createButton({
        title: title,
        height: 37,
        width: 37
    });
    menuBtns.push(button);
    v = new HomeView({menuBtn: button, id: id});
    alert('title: ' + title); // --> 1 through 6
    data.push({
        title: title,
        view: v,
        dataType: 'dashboardapplication'
    });
}

alert('data length: ' + data.length); // --> 6
相关问题