widget.newTableView() is split into four components you need to use to “fill” it. Just calling the command by itself does nothing because the table at that point is empty. (You can see how to fill it at the bottom of the docs page.)
Basically, you need to pay attention to two parts: (1) the render function, and (2) the table you are trying to use. Here’s an example of using a table with the widget render function.
-- My sample table I want to put in the view
local animals = {}
animals[1] = { "cat", 10 }
animals[2] = { "dog", 23 }
animals[3] = { "gopher", 54 }
animals[4] = { "horse", 27 }
animals[5] = { "crab", 14 }
animals[6] = { "beetle", 99 }
animals[7] = { "unicorn", 1 }
The trick is that your table needs to follow the order needed by your widget. If it doesn’t, you need to make a *new* table that inserts data from your *old* table into a more friendly form.
[code]-- onRender listener for the tableView
local function onRowRender( event )
local row = event.target
local rowGroup = event.view
– This will display “Animal: cat” on the first row. event.index is the row #
– The [1] after event.index says to use the first value within that row.
local text = display.newText( rowGroup, "Animal: " … animals[event.index][1], 12, 0, “Helvetica-Bold”, 18 )
text:setReferencePoint( display.CenterLeftReferencePoint )
text.y = row.height * 0.5
text:setTextColor( 0 )
– This will display “How many? 10” as a sort of subtitle
local count = display.newText( rowGroup, "How many? " … animals[event.index][2], 12, 0, “Helvetica-Bold”, 18)
count:setReferencePoint( display.CenterLeftReferencePoint)
count.y = row.height * 0.7
count:setTextColor( 0 )
end[/code]
The second thing you have to do is only insert as many rows as your table has!
[code]-- insert row objects into the tableView
for i = 1, #animals do – “for as many rows as exist in animals{}”
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] [import]uid: 41884 topic_id: 35694 reply_id: 141977[/import]