Filling List through Table using TableView

I want to use TableView to create my list but as I searched it here, TableView use onRender for rendering the rows but I want TableView to take a table from me that contains text needed to be render in form of titles and subtitles. But when I uselocal list = widget.newTableView{}, none of the argument takes data that I want to display.

May be this one is too basic but I am newbie here and need help
Thanks [import]uid: 173824 topic_id: 35694 reply_id: 335694[/import]

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]

Thank you richard9

Can I reference these list items in its listener.
My problem is that I am adding a title,subtitle and a button in each row.
Title and Subtitle are in a table but I am adding the button individually in each row.

local function onRowRender( event )
local row = event.target
local rowGroup = event.view
local label = rowTitles[row.index]
print(label.title)

local title = display.newText( label.title , 0, 0, native.systemFontBold, 25 )
title:setReferencePoint( display.CenterLeftReferencePoint )

title:setTextColor(255)
rowGroup:insert(title)
title.x = rowGroup.contentWidth*0.05
title.y = rowGroup.contentHeight * 0.3
row.title = title – I am making a reference here and then trying to get this in onRowTouch
local subtitle = display.newText( label.subtitle, 0, 0, native.systemFont, 18 )
subtitle:setReferencePoint( display.CenterLeftReferencePoint )
subtitle:setTextColor(255)

rowGroup:insert(subtitle)
subtitle.x = rowGroup.contentWidth*0.05
subtitle.y = rowGroup.contentHeight*0.8
row.subtitle = subtitle – I am making a reference here and then trying to get this in onRowTouch
–Setup the delete button
delbutton = widget.newButton{
style = “blue1Small”,
label = “DELETE” ,
onPress = onDeletePress
}

delbutton.x = display.contentWidth*0.95
delbutton.y = rowGroup.contentHeight*0.7
delbutton.isVisible = false
rowGroup:insert(delbutton)
row.deletebutton = delbutton
– I am making a reference here and then trying to get this in onRowTouch

end

Now in onRowTouch what should I do to access my button as I want to set its visibility true there??

Thanks in advance

[import]uid: 173824 topic_id: 35694 reply_id: 142054[/import]

I got it
Above code is right if you want to get access to all these items.
In my listener, I just added the line
row.deletebutton.isVisible = true
and it worked :slight_smile: [import]uid: 173824 topic_id: 35694 reply_id: 142055[/import]

I got it
Above code is right if you want to get access to all these items.
In my listener, I just added the line
row.deletebutton.isVisible = true
and it worked :slight_smile: [import]uid: 173824 topic_id: 35694 reply_id: 142056[/import]

You can make your code look like code by adding < code > < / code > (no spaces) around your code chunk. (for this forum)

Glad its working! [import]uid: 41884 topic_id: 35694 reply_id: 142057[/import]

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]

Thank you richard9

Can I reference these list items in its listener.
My problem is that I am adding a title,subtitle and a button in each row.
Title and Subtitle are in a table but I am adding the button individually in each row.

local function onRowRender( event )
local row = event.target
local rowGroup = event.view
local label = rowTitles[row.index]
print(label.title)

local title = display.newText( label.title , 0, 0, native.systemFontBold, 25 )
title:setReferencePoint( display.CenterLeftReferencePoint )

title:setTextColor(255)
rowGroup:insert(title)
title.x = rowGroup.contentWidth*0.05
title.y = rowGroup.contentHeight * 0.3
row.title = title – I am making a reference here and then trying to get this in onRowTouch
local subtitle = display.newText( label.subtitle, 0, 0, native.systemFont, 18 )
subtitle:setReferencePoint( display.CenterLeftReferencePoint )
subtitle:setTextColor(255)

rowGroup:insert(subtitle)
subtitle.x = rowGroup.contentWidth*0.05
subtitle.y = rowGroup.contentHeight*0.8
row.subtitle = subtitle – I am making a reference here and then trying to get this in onRowTouch
–Setup the delete button
delbutton = widget.newButton{
style = “blue1Small”,
label = “DELETE” ,
onPress = onDeletePress
}

delbutton.x = display.contentWidth*0.95
delbutton.y = rowGroup.contentHeight*0.7
delbutton.isVisible = false
rowGroup:insert(delbutton)
row.deletebutton = delbutton
– I am making a reference here and then trying to get this in onRowTouch

end

Now in onRowTouch what should I do to access my button as I want to set its visibility true there??

Thanks in advance

[import]uid: 173824 topic_id: 35694 reply_id: 142054[/import]

I got it
Above code is right if you want to get access to all these items.
In my listener, I just added the line
row.deletebutton.isVisible = true
and it worked :slight_smile: [import]uid: 173824 topic_id: 35694 reply_id: 142055[/import]

I got it
Above code is right if you want to get access to all these items.
In my listener, I just added the line
row.deletebutton.isVisible = true
and it worked :slight_smile: [import]uid: 173824 topic_id: 35694 reply_id: 142056[/import]

You can make your code look like code by adding < code > < / code > (no spaces) around your code chunk. (for this forum)

Glad its working! [import]uid: 41884 topic_id: 35694 reply_id: 142057[/import]