In this post I will try to explain how to create custom rows for TableViews using Titanium Mobile.
If you don’t know what Titanium is about, take a look here. I suppose you already know how to setup and start a Titanium project so I’ll show only how to create a custom row.
By default a TableView will let you create a row that can have: the left image, the title and the row type decorator set. The next code is an example of the standard data that needs to be passed to a TableView.
var RegData = [ { leftImage:'es.png', title:"Spain", hasChild:true }, { leftImage:'gb.png', title:"United Kingdom", hasChild:true }, { leftImage:'us.png', title:"United States", hasChild:true }, { leftImage:'fr.png', title:"France", hasChild:true } ]; var TheTable = Titanium.UI.createTableView({ data:RegData });
And the result is this:
Ok, we would like to add more data to a row, maybe something like this:
This will require to build the row “by hand”. So we need to add a 2 imageViews, the flag and the trend, and labelViews for the country and the percent . We also need to change the data array of course.
var CustomData = [ { flag:'es.png', country:"Spain", trend:'up.png', percent:'28%' ,hasChild:true }, { flag:'gb.png', country:"United Kingdom", trend:'down.png', percent:'-3%', hasChild:true }, { flag:'us.png', country:"United States", trend:'up.png', percent:'8%', hasChild:true }, { flag:'fr.png', country:"France", trend:'down.png', percent:'-40%', hasChild:true } ];
We create a data variable as an array that will hold the row objects generated.
var data=[];
then we walk through the CustomData array, create a new row , the imageViews and labels and add them to the row.
for (var i = CustomData.length - 1; i >= 0; i--) { var row = Titanium.UI.createTableViewRow(); var flag = Titanium.UI.createImageView({ url:CustomData[i].flag, width:32, height:32, left:4, top:2 }); var country = Titanium.UI.createLabel({ text:CustomData[i].country, font:{fontSize:16,fontWeight:'bold'}, width:'auto', textAlign:'left', top:2, left:40, height:16 }); var percent = Titanium.UI.createLabel({ text:CustomData[i].percent, font:{fontSize:12,fontWeight:'bold'}, width:'auto', textAlign:'left', bottom:0, left:60, height:12 }); var trend = Titanium.UI.createImageView({ url:CustomData[i].trend, width:16, height:16, right:10 }); row.add(flag); row.add(country); row.add(percent); row.add(trend); row.hasChild=CustomData[i].hasChild; row.className = 'coutry_row'; data.push(row); };
As you see we also add a className to the row to improve the rendering performance, as the iPhone will reuse the row template with every new data when rendering the table.
The obtained result is this:
You can download the complete Titanium project from here. You will have to create a new project in Titanium and replace the resource folder with the one in the archive.
Let me know your thoughts.
Check the Spanish version of this post:
Como crear filas personalizadas para TableViews usando Titanium Móvil



28 Responses
Thanks, that’s really great to share this.
Nice tutorial!
Well done, thanks for sharing. The classname trick is wise and save ressources.
Thanks for the great tutorial!!
Everything is working for me except I cannot get an Event Listener to link up with the data in order to open new windows when a table selection is made. I have other regular tables that open windows just fine but for some reason I cannot get the event listener to read the data. (I used an alert dialog from kitchen sink and the table does listen just fine, showing the dialog).
Thanks and I can’t wait for more of your Titanium tutorials!
Hi, can you post some code on how you create the rows and the table and how you try to get the event?
I need this to see what you are doing there
Try in this lines:
// create table view event listenertableview.addEventListener(‘click’, function(e)
{
var newwin = Titanium.UI.createWindow({
url:e.rowData.test,
title:e.rowData.date
});
Titanium.UI.currentTab.open(newwin,{animated:true});
});
to add some logging to see if you get the rowData,
Titanium.API.info(e.rowData);and try simply with
newwin.open({transition:Titanium.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT})instead of trying to get the current tab.
To be hones I’ve never been able to use right currentWindow and currentTab.
A solution would be to make your own stack of tabs and take it from there.
I added this line
Titanium.API.info(e.rowData);
and in the Titanium log screen it returns
[INFO] [object TiUITableViewRow]
Is it supposed to return more info than that if it is working?
try
Titanium.API.info(e.source.rowData);or
Titanium.API.info(e.rowData.rowData);and let me know.
If none is working I will import your code in a Titanium project and try to figure out what’s going on.
I’m waiting for your reply
Hey Dan,
Thanks for the awesome customer service
I tried both of those and the log returns
[INFO] null
Ok, I’ll take a deeper look at your code and let you know.
i just deleted the code
, can you send it over email to dan.tamas [at] gmail [dot] com ?
I found it
you forgot to add the test and date fields to your row
Add this when you build the row
row.test = customData[i].test;row.date = customData[i].date;
Another thing would be to try to make the variables more clear, you have another date variable that is a label and may confuse you ( or maybe this was the reason you lost so much time
).
let me know
OMIGOD I love you so much Dan!!!! Thank you!!! I figured it had to be something like that but couldn’t put my finger on it… awwww what a relief
Great tip – just what I was looking for.
I needed a way to add the same data to EVERY row, like background color, etc
thanks
Hi,
great tutorial, fab to get more data in the table, but am having the same problem as appceleron when I click on an item I get null for my alerts – I downloaded your project and put an alert in the ‘click’ listener and get the same – help!
not sure what you mean in your reply to appceleron about row.test and row.date as his code was not posted)
Any help much appreciated!
Can you post some code on http://pastie.org so I can take a look at your code?
Thanks, but I think I’ve fixed it – got sidetracked by the alerts suggested above – they both return null, but if I alert just on e.index I get the row ID
Add to that the fact I had changed the name of one of the columns probably accounts for it!
So my bad and your cool code
Thanks for the uber fast response – any other crafty tutorials in the pipeline (like how to make pop up window work in iPad split view landscape mode (as per kitchen sink windows standalone (animation fun)) – works fine in portrait, but still appears as portrait when device in landscape (so come s up sideways!)
sorry off topic, but doing my head in!
I’m working on a countdown example for titanium, with a little of OOP for javascript
, but you could ask on the Appcelerator list
For the popup window I don’t know yet, I didn’t need it so far
http://developer.appcelerator.com/questions/created
Will do – thanks
looking forward to the countdown
Awesome tutorial, I was trying to figure out from the api how to get this working and your tutorial outlined it nicely! Thanks!
Ok guys, two more tutorials released:
iPhone countdown timer with Titanium
and
Combobox control for iPhone – this one has video too.
Let me know your thoughts
Awesome!
How do I set the searchBar to search by the label in the custom row?
Sorry for the late reply.
The only way to do this I think is to manage yourself the searching, based on the labels text, and show only the rows you want.
So you register all the labels in a separate array to avoid accessing the rows in the table – this can put a big load on the phone, search inside this array and display the correspondent table rows only.
I don’t have another idea.
[...]Custom row for TableView in Appcelerator Titanium[...]
[...] Custom Table View rows [...]
ya men
pls given some example of blackberry for this type.
Thanks for this great tutorial! It was very helpful for me.
One tip: use the image property in createImageView instead of the deprecated property url. With url, there are some repaint problems, if you add or remove rows.
I’m sorry but BB it’s beta and I don’t have yet access to it. If you have bought the pro pack you should be able to talk to the team if you have issues.