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 = [

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

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

    alert('"' + e.source.title + '" row clicked');


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

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

        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
    • 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.