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):
- an alert that says [rows[0] is the "foo" row]
- an alert that says ["foo" row clicked]
Actual result(s):
- an alert that says [rows[0] is the "foo" row]
- 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.
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); });