widget.newTableView() is something of a bear to get loaded initially since it’s not a single function call.
Realistically what you should do is build the exact bare minimum to construct a working tableView and then build onto it from there. (The API code works but adds some elaboration for “how do I do this?” purposes)
Assuming you have already required in the widget library, here’s what the bare minimum should look like:
[code]local options = { top=display.statusBarHeight } – maybe don’t even need to pass options, but…
– (1) This calls the list into being.
local list = widget.newTableView(options)
– (2) Whenever you touch a row this is called.
local function onRowTouch( event )
local row, rowGroup = event.target, event.view
if event.phase == “press” then
if not row.isCategory then rowGroup.alpha = 0.5; end
elseif event.phase == “release” then
if not row.isCategory then
– reRender property tells row to refresh if still onScreen when content moves
row.reRender = true
end
end
return true
end
– (3) This is called to render each row.
local function onRowRender( event )
end
– (4) This is called to generate the actual list - everything from this for loop is as important as a function.
for i = 1, #numrows do
local rowHeight, rowColor, lineColor, isCategory
– function below is responsible for creating the row
list:insertRow{ onEvent=onRowTouch, onRender=onRowRender, height=rowHeight, isCategory=isCategory, rowColor=rowColor, lineColor=lineColor }
end[/code]
That’s it. You could probably shorten it even more, but it’s not even really functional as is - I’m not sure if this actually works without inserting something into the rows.
Anyway, what you really need to do is feed a table so that “#numrows” bit works. The best way to do this is to actually feed it a table!
[code]-- My favorite colors
local colors = {}
colors[1] = { isCategory=true, name=“Colors List” }
colors[2] = { name=“Purple”, flavor=“Grape” }
colors[3] = { name=“Blue”, flavor=“Mountain Dew” }
– etc…
for i = 1, #colors do
local rowHeight, rowColor, lineColor, isCategory
if colors[i].isCategory then
isCategory = true
rowHeight = 24
elseif colors[i].flavor == “blue” then
rowColor = { 0, 0, 255 }
end
list:insertRow{ onEvent=onRowTouch, onRender=onRowRender, height=rowHeight, isCategory=isCategory, rowColor=rowColor, lineColor=lineColor }
end
[/code]
Why do you want a table with headers done first? Well apart from above (making the for loop really easy), it means you can call from that same table in onRowRender() - it’s just that instead of “i” you can use event.index.
Now to your specific question, you can solve that by:
- add functions to that table. For example if you had a function called "showColor(), you could type
colors[3].function = showColor -- just the name, not the ()
And then during on onRowTouch()
if event.phase == "ended" then
colors[event.index].function() -- will call the function at that index, so if event.index = 3, then it runs showColor()
(Just keep in mind you would have to declare colours{} before the onRowTouch function or it will come up as nil) [import]uid: 41884 topic_id: 29205 reply_id: 117452[/import]