Skip to content

Instantly share code, notes, and snippets.

@egomez99
Created January 25, 2012 17:59
Show Gist options
  • Save egomez99/1677610 to your computer and use it in GitHub Desktop.
Save egomez99/1677610 to your computer and use it in GitHub Desktop.
Common methods in TableViews
var win = Ti.UI.createWindow();
win.backgroundColor = '#EEE';
var tableView = Ti.UI.createTableView({
width: 300,
height: 200,
top: 155,
left: 10,
backgroundColor: '#AAA'
});
var addRow = Ti.UI.createButton({
title: 'Add row',
width: 140,
top:5,
left: 10,
height: 50,
fontSize: 16
});
var delRow = Ti.UI.createButton({
title: 'Delete row',
width: 140,
top:5,
height: 50,
left: 160,
fontSize: 16
});
var fillTable = Ti.UI.createButton({
title: 'Fill table',
width: 140,
height: 50,
top:60,
left: 10,
fontSize: 16
});
var label1 = Ti.UI.createLabel({
top: 115,
left: 53,
width: 100,
height: 30,
text: 'Row index:',
});
var tf1= Ti.UI.createTextField({
width: 50,
height: 30,
top:115,
backgroundColor:'white',
left: 178,
value: '1'
});
var clearButton = Ti.UI.createButton({
title:'Clear Table',
height:50,
width:140,
top:60,
left:160,
fontSize: 16
});
clearButton.addEventListener('click', function(){
tableView.setData([]);
});
win.add(clearButton);
win.add(fillTable);
win.add(tableView);
win.add(addRow);
win.add(delRow);
win.add(tf1);
win.add(label1);
var i = 1;
var newRow = function (){
row = Ti.UI.createTableViewRow({
title: 'This is row #' + i
});
i++;
};
addRow.addEventListener('click', function() {
//Case#1
newRow();//create a New Row
tableView.appendRow(row);//appendRow then
//Case#2
/*
tableView.appendRow({
title : 'Foo'
}, {
animationStyle : Titanium.UI.iPhone.RowAnimationStyle.LEFT
});
//NOTE: since we're appending 2 different row layouts, we need to give one of them
//a table className otherwise the tableview will assume they're the same layout and
//you'll get warnings and bad performance on lots of rows - this shows you how to do that
var row = Ti.UI.createTableViewRow({
height : 50,
className : 'row'
});
var label = Ti.UI.createLabel({
text : 'row 1',
color : '#111',
width : 'auto',
height : 'auto'
});
row.add(label);
tableView.appendRow(row, {
animationStyle : Titanium.UI.iPhone.RowAnimationStyle.LEFT
});
*/
});
delRow.addEventListener('click', function(){
tableView.deleteRow(parseInt(tf1.value)-1);
});
fillTable.addEventListener('click',function(){
var data = [{title:'This is row #1'},
{title:'This is row #2'},
{title:'This is row #3'},
{title:'This is row #4'},
{title:'This is row #5'}];
i=6;
tableView.setData(data);
});
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment