Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Adding an event handler to a TableViewRow after creation

I am attempting to programmatically add a 'click' event handler to a TableViewRow after the TableView has been created. To achieve this, I navigate the TableView object to locate the row object and attach a 'click' event handler to that object. However, when I click the row, the event doesn't fire. In the code below, I have even hard-coded the event to fire on the same object to which the event is attached, but the event never fires.

data = [
    {title:'foo',hasChild:true},
    {title:'bar',hasChild:true},
];

var tableView = Titanium.UI.createTableView({data:data});
var window =    Titanium.UI.createWindow();

alert('rows[0] is the "' + tableView.data[0].rows[0].title + '" row');

tableView.data[0].rows[0].addEventListener('click',function(e)
{
    alert('"' + e.source.title + '" row clicked');
});

window.add(tableView);
window.open();
tableView.data[0].rows[0].fireEvent('click');

Expected result(s):

  1. an alert that says [rows[0] is the "foo" row]
  2. an alert that says ["foo" row clicked]

Actual result(s):

  1. an alert that says [rows[0] is the "foo" row]
  2. no alert that says ["foo" row clicked]

I'm guessing tableView.data[0].rows[0] is just a proxy for the 'actual' row object (i.e., the object that can receive Titanium events), so how do I get the actual row object from the TableView? Is this even possible? All of the example code I see either attaches an event listener to a row before adding it to the TableView, or adds a listener to the entire TableView.

— asked November 17th 2010 by Frank Battaglia
  • addeventlistener
  • click
  • mobile
  • tableviewrow
0 Comments

1 Answer

  • i think you can solve you problem with just an event listener on the whole table and get the index of the item clicked in the event handler

    tableView..addEventListener('click',function(e)
    {
        Ti.API.info("clicked row number " + data.index);
        Ti.API.info("clicked row object " + data.row);
        Ti.API.info("clicked row title " + data.row.title);
    });
    
    — answered November 17th 2010 by Aaron Saunders
    permalink
    3 Comments
    • I want each row to have a distinct handler that can be modified. If I use one handler for the entire TableView as you suggest, modifying the handler for each row gets pretty ugly. Right now I'm thinking of an array of functions (indexed by a row id) associated with the TableView, and using the TableView event handler to call the proper function from the array, but that seems very kludgy. Is there really no better way to have individual handlers for each row?

      — commented November 17th 2010 by Frank Battaglia
    • tableView..addEventListener doesn't work for me!
      => parse error

      — commented November 24th 2010 by Andreas Schauer
    • sorry should only be one period

      — commented February 2nd 2011 by Aaron Saunders
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.