Skip to content

Instantly share code, notes, and snippets.

@iskugor
Created February 2, 2012 13:12
Show Gist options
  • Save iskugor/1723400 to your computer and use it in GitHub Desktop.
Save iskugor/1723400 to your computer and use it in GitHub Desktop.
How to get table view row object from row index in Titanium
var win = Titanium.UI.createWindow({
title:'Tab 1',
backgroundColor:'#fff'
});
var aTableView = Ti.UI.createTableView();
var data = [
Ti.UI.createTableViewRow({title:'Row 1', hasChild:true, color:'red', header:'First'}),
Ti.UI.createTableViewRow({title:'Row 2', hasDetail:true, color:'green'}),
Ti.UI.createTableViewRow({title:'Row 3', hasDetail:true, color:'green'}),
Ti.UI.createTableViewRow({title:'Row 4', hasCheck:true, color:'blue', header:'Second'}),
Ti.UI.createTableViewRow({title:'Row 5', color:'orange'})
];
aTableView.setData(data);
aTableView.addEventListener('click', function(e) {
Ti.API.info('Clicked index: ' + e.index);
});
win.add(aTableView);
function getTableViewRowFromIndex(table, index) {
Ti.API.info('index: ' + index);
var sections = table.data;
if (!sections) {
return null;
}
var currentRowIndex = index, row;
for (var i = 0; i < sections.length; ++i) {
Ti.API.info('Section: ' + i);
Ti.API.info('Section length: ' + sections[i].rows.length);
Ti.API.info('currentRowIndex: ' + currentRowIndex);
if (currentRowIndex < sections[i].rows.length) {
Ti.API.info('RETURNING: ' + sections[i].rows[currentRowIndex].title);
Ti.API.debug(sections[i].rows[currentRowIndex]);
Ti.API.debug(JSON.stringify(sections[i].rows[currentRowIndex]));
return sections[i].rows[currentRowIndex];
}
else {
currentRowIndex -= sections[i].rows.length;
}
}
}
win.addEventListener('open', function() {
var row = getTableViewRowFromIndex(aTableView, 4);
//Ti.API.info('Got: ' + row.getTitle());
});
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment