钛的范围问题

时间:2013-11-25 18:21:15

标签: javascript arrays scope titanium

我在下面创建的tfield []数组中遇到了范围问题。我正在用六个单元格创建十行。每个单元都附有一个监听器以进行验证。我需要对每一行求和单元格并创建一个运行总计进行计算。当我调用一个例程来对单元格求和时,我的单元格变量tfield [x] .value是空白或未定义的,这取决于我最初声明tfield []的位置。

function createRow1(i) // start create row 
{ 

  row1 = Ti.UI.createView({
    backgroundColor: 'white',
    borderColor: '#bbb',
    borderWidth: 1,
    width:'100%', height: 70,
    top: 0, left: 0 });

    var tfield1 = [];
    var label1 =  [];
    var label2 = []; 

    for (i=0;i<6;i++)
    {
       tfield1[i] = Ti.UI.createTextField(baseAttrs); 
       label1[i] = Ti.UI.createLabel(lbAttrs1);
       label2[i] = Ti.UI.createLabel(lbAttrs2);
    }


    tfield1[0].addEventListener('change', function()
    {
       if (tfield1[0].value > 10)
       {
          tfield1[0].value = "";  
          showMessageTimeout("More than 10.",15);
        }
     }); 

   // 6 listeners created, one for each element in the
   // tfield[] array. Omitted for brevity.     

   tfield1[0].left ="0%"; 
   tfield1[1].left ="12%";
   tfield1[2].left ="24%";
   tfield1[3].left ="36%";
   tfield1[4].left ="48%";
   tfield1[5].left ="60%";



   for (i=0;i<6;i++)
   {
      row1.add(tfield1[i]);
      row1.add(label1[i]);
      row1.add(label2[i]);
   }

  return row1;

}  /// end of createrow1

  for(i = 0; i < 10; i++){
    row1 = createRow1(i);
    scrollView1.add(row1);
  }

/*
     all six rows are added to a scrollview. A button 
     click fires a listener that sums each row which  
     should also calc a running total of all rows. When 
     called the calc function either shows tfield[] 
     as undeclared or as a blank, depending on 
     where tfield[] is declared.
  */

1 个答案:

答案 0 :(得分:0)

我正在回答你的上一个问题,但它被删除了。

首先,你现在的问题不像以前那么清楚。我认为缺乏代码。

但别担心,我已经发布了所有代码。所以:

你真正在做一个元素矩阵,我认为你可以轻松访问所有textField元素:

var tfields = [];

buttonCalc.addEventListener('click',function(e)
{
   showMessageTimeout("calc pressed",15);
   alert(tfields[0][0].value);
   // alert(tfield1[0].value);  // reference to tfield[0] blows up.

});

...

function createRow1(i) // start create row 
{ 
    ...
    tfields.push(tfield1);  //Add this line here
    return row1;

}  /// end of createrow1

我已经回答了所有其他问题,你可以向他们提出一个新问题: - )