2012-02-02 4 views
0

희망이 있습니다, 나는 티타늄 모바일 아이폰 애플 리케이션을 구축하는 데 사용하고 있습니다. 각 항목에 DishID와 DishTitle이있는 100 개의 항목이 배열되어 있으며, DishTitle을 tableview에 표시하고 이벤트 리스너에서 DishID를 전달해야하며 이벤트 리스너에 대해 나중에 경고합니다. 항목의 ID이 내 코드는 지금까지 있습니다 :티타늄 모바일 루프에서 이벤트 리스너에서 변수를 가져옵니다

var dishes = eval(this.responseText); 

for (var i = 0; i < dishes.length; i++) 
      { 

       DishID[i] = dishes[i].DishID; 

       var row = Ti.UI.createTableViewRow(); 
        row.selectedBackgroundColor = '#fff'; 
        row.height = 30; 
        row.className = 'datarow'; 
        row.clickName = 'row'; 

       // Create the label to hold the screen name 
       name[i] = Titanium.UI.createLabel({ 
        color:'#000', 
        font:{fontSize:16,fontWeight:'bold', fontFamily:'Arial'}, 
        left:5, 
        top:2, 
        height:30, 
        width:200, 
        text:dishes[i].DishTitle 
       }); 

       name[i].addEventListener('click', function(e){ 

           alert(DishID[i]); 
          }); 

      } 
내가 상관없이 내가 클릭하는 라벨 동일한 ID (208)를 받고 없습니다 유지 데 문제는, 내가 뭔가 잘못하고있는 중이 야

?

+0

'요리'가 초기화되는 방법을 알고 싶습니다. 다른 ID라고 확인 했습니까? – penguin4hire

+0

님이 포스트를 편집했습니다 : –

답변

1

범위/폐쇄 문제가 있습니다. '클릭'이 발생하면 i = 208이됩니다. eventListener를 테이블에 놓고 행의 사용자 정의 속성을 넣는 것이 가장 좋습니다.

var table = Ti.UI.createTableView(); 
var rows = []; 

for(var i = 0; i < dishes.length; i++) { 

    var row = Ti.UI.createTableViewRow({ 
     selectedBackgroundColor : '#fff', 
     height : 30, 
     className : 'datarow' 
    }); 

    row.dishId = dishes[i].DishID; 

    // Create the label to hold the screen name 
    var label = Ti.UI.createLabel({ 
     color : '#000', 
     font : { 
      fontSize : 16, 
      fontWeight : 'bold', 
      fontFamily : 'Arial' 
     }, 
     left : 5, 
     top : 2, 
     height : 30, 
     width : 200, 
     text : dishes[i].DishTitle 
    }); 

    row.add(label) 

    rows.push(row); 
} 

table.setData(rows); 

table.addEventListener('click', function(e) { 
    alert(e.row.dishId); 
}); 
+0

지연에 대한 미안하지만, 정말 고마워요. :) –

관련 문제