user2363025 user2363025 - 1 year ago 107
Javascript Question

Accessing custom rows of a TableView

I am pushing items to a data array and then adding these to a rowData array in order to create custom rows for a table. I need to know how to access these when a user clicks a specific row. I was previously using e.rowData, title etc. for accessing these elements when I had a basic table but now that it is a custom table, this isn't working. Don't worry about the parsing bit, that all works fine. All help appreciated!


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,


var rowData=[];

var img= Titanium.UI.createImageView({
height: 100,
width: 100

var bgBar=Titanium.UI.createView({
width: "100%",
backgroundColour: "#000",
opacity: 0.6

var title=Titanium.UI.createLabel({
color: 'black',
left: 105

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


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


tableView.addEventListener("click", function (e){
console.log("HERE SOURCE.TITLE ------------------"+e.row.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);

Answer Source

There are two ways to set the rows of a table, you can create custom rows, or create JSON objects that have the attributes of the row object. Both cases have different ways of accessing the row, either the row object or the rowData object, from the docs:

When the row is created implicitly using a JavaScript dictionary object, use [the rowData property] rather than row to access any custom row properties. Here's an example of creating a row implicitly, which is not the recommended way.

In your example, since you did not create the row objects implicitly, but explicitly, you can access the clicked row from the row attribute of the event like this:

tableView.addEventListener("click", function(e){
    // Get the [TableViewRow](!/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;

Alternatively, in both cases, you could always use the index to look up the row object like this:

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 =[ndx];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download