2013-07-18 2 views
1

테이블에 대한 사용자 정의 행을 작성하기 위해 항목을 데이터 배열로 푸시 한 다음이를 rowData 배열에 추가합니다. 사용자가 특정 행을 클릭 할 때 액세스하는 방법을 알아야합니다. 이전에 e.rowData, title 등을 사용하여 기본 테이블을 가지고 있었지만이 테이블이 사용자 정의 테이블 일 때이 요소에 액세스 할 수있었습니다. 이것이 작동하지 않습니다. 구문 분석 비트에 대해 걱정하지 마십시오. 모두 잘 작동합니다. 모든 도움을 주셔서 감사합니다!TableView의 사용자 정의 행 액세스

data.push({ 

      title: items.item(i).getElementsByTagName("title").item(0).text,    
     leftImage: str.match(patt1) !== null ? str.match(patt1)[0] : 'image_news.png', 
      dataToPass: items.item(i).getElementsByTagName("description").item(0).text, 
      hasChild: true, 
      js:"external.js" 
     }); 



    } 

    var rowData=[]; 

    for(i=0;i<data.length;i++){ 
     var img= Titanium.UI.createImageView({ 
      image:data[i].leftImage, 
      left:0, 
      bottom:5, 
      height: 100, 
      width: 100 
     }); 

     var bgBar=Titanium.UI.createView({ 
      height:110, 
      width: "100%", 
      bottom:0, 
      left:0, 
      backgroundColour: "#000", 
      opacity: 0.6 
     }); 

     var title=Titanium.UI.createLabel({ 
      text:data[i].title, 
      color: 'black', 
      left: 105 
     }); 

     var row=Titanium.UI.createTableViewRow({ 
      height: "auto", 
      hasChild: "true", 
     js:"external.js", 
     dataToPass: data[i].dataToPass 
     }); 

     row.add(img); 
     row.add(bgBar); 
     row.add(title); 



     rowData.push(row); 
     console.log("row shiznick" + rowData); 
    } 


    tableView.setData(rowData); 


tableView.addEventListener("click", function (e){ 
     console.log("HERE SOURCE.TITLE ------------------"+e.row.title); 
     console.log("YOYOOYOYO------------"+e.source.title); 
     console.log("IS THIS IT---------------"+e.children[0]); 
     console.log("HERE IS THE SOURCE -----------------------------"+ e.source); 
     console.log("HERE SOURCE.LEFTIMAGE REG EXP3 ------------------"+e.row.leftImage); 
     console.log("HERE SOURCE.ClassName ------------------"+e.source.className); 
     console.log("HERE HASCHILD ------------------"+e.rowData.hasChild); 
     console.log("HERE DATATOPASS ------------------"+e.row.dataToPass); 
}); 

답변

2

테이블의 행을 설정하는 방법은 두 가지가 있습니다, 사용자 정의 행을 만들거나이 JSON 개체를 만들 수 있습니다 행 객체의 속성.

행이 자바 스크립트 사전 개체를 사용하여 암시 적으로 만들 때 [rowData 하행 속성]을 사용하기보다는 행에 액세스하려면 다음 두 가지 경우는 row 개체 또는 rowData 객체 from the docs 중 하나를 행을 액세스하는 다른 방법이 모든 사용자 정의 행 속성 다음은 암시 적으로 행을 만드는 예제입니다. 권장되는 방법은 아닙니다. 모두,

tableView.addEventListener("click", function(e){ 
    // Get the [TableViewRow](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.TableViewRow) object 
    var theRow = e.row; 
    // Get the children 
    var rowChildren = theRow.children; 
    // Here are your objects in the row 
    var imgInRow = rowChildren[0]; 
    var bgBarInRow = rowChildren[1]; 
    var titleInRow = rowChildren[2]; 
    // Here is the title 
    var rowTitle = titleInRow.text; 
}); 

또는 : 당신이 암시하지만, 명시 적으로 행 객체를 생성하지 않았기 때문에 당신의 예에서

는이 같은 이벤트의 row 속성에서 클릭 한 행에 액세스 할 수 있습니다 사용자 (사용자 정의 행의 경우에 표시되지 않는 심지어를 클릭하면 행의 datas에 액세스하기위한

tableView.addEventListener("click", function(e){ 
    // Get the index of the row 
    var ndx = e.index; 
    // Get the row, this only works if you have not added sections 
    var row = tableView.data[ndx]; 
}); 
+0

대단히 감사합니다. 내 유일한 문제는 dataToPass에 액세스하는 방법을 확신 할 수 없다는 것입니다. 나는 시도했다 \t var dataToPassInRow = rowChildren [3]; 및 dataToPassInRow.text하지만이 오류가 발생하고 있습니까? 저는 다른 사람들과 마찬가지로이 속성을 추가하지 않았기 때문이라고 생각합니다. 내가 빠진 것이 있습니까? – user2363025

+0

또한 이미지에 액세스하기 위해 undefined를 반환하는 console.log (imgInRow.text)를 시도했습니다. console.log (imgInRow.image)하지만 [object imageView]를 반환하고 console.log (imgInRow.image.text)를 시도했지만 정의되지 않은 값이 반환됩니다 – user2363025

+0

hmm e.rowData가 dataToPass에서 작동하는 것 같습니다. 이것은 혼란 스럽다. – user2363025

-1

, 뭔가 같은 event.taget 기능을 사용해보십시오 :

$(".row").click(function(event) { 
    var element = event.target; 
    // Accessing element 
}); 
+0

액세스 JQuery와이다? – user2363025

+0

아니요, javascript로 dom 속성에 액세스합니다. – Hernandcb

+0

으로 인해 앱이 다운됩니다. 특정 오류 메시지가 나타나지 않습니다. – user2363025

1

: 경우, 당신은 항상이 같은 행 개체를 찾기 위해 인덱스를 사용할 수 예를 들어) 배열을 만들고 루프 중에 데이터를 밀어 넣고 e.index를 사용하여 배열에 액세스 할 수 있습니다. 간단한 코드 예 :

var allDataArray = []; 

     // loop through each item 
     for (var i = 0; i < items.length; i++) 
     { 
      // Your source datas (custom row : id is not displayed in the rows) 
      var sourceData = { 
       id: items.item(i).getAttribute('id'),    
       title: items.item(i).getElementsByTagName("title").item(0).textContent, 
      }; 

      // Saving data for each loop 
      allDataArray.push(sourceData); 
} 

가 다음 데이터

itemsTable.addEventListener('click', function(e) 
     { 
      // Target the array with e.index offset 
      var rowContent = allDataArray[e.index]; 
      Ti.API.info(rowContent); // will display every single data of the current row clicked 
      alert(rowContent.id) // will display the "hidden" id which was not displayed in the UI 

} 
관련 문제