How to add data to a tableview widget?

Hi everyone…

I have been trying to find a way to add data to my table view…

I have seen few videos and samples, and I just can not make it work.

The jay white videos, a few tutorials on Corona, on the forum I see widget 1.0

or old tutorials…

So far I have the table view with rows and text … row1, row2. row3, and so forth…

How do I add my own text?

even Jay said, that was a nightmare…

is it so difficult?

I follow the Jays video…

local top = display.statusBarHeight local listRecs = {} local nameData = {"Smith", "Williams", "Jones", "Brown", "Miller", "Smith", "Williams", "Jones", "Brown", "Miller","Smith", "Williams", "Jones", "Brown", "Miller","Smith", "Williams", "Jones", "Brown", "Miller"} local function setup()     local bg = display.newRect(240, 180, display.contentWidth, display.contentHeight - top)     bg:setFillColor(0, .67, .29)     list = widget.newTableView {         top = top + 10,         height = 304     } end local function loadData()     for x = 1, #nameData do         listRecs[x] = {}         listRecs[x].name = nameData[x]         listRecs[x].age = math.random(18, 35)         listRecs[x].showDel = false     end end local function showRecords()     local function onRowRender (event)         local row = event.row         local rowGroup = event.view         local idx = row.index or 0         local color = 0                  row.textObj = display.newText( listRecs[idx].name, 0, 0, "Helvetica", 14)         row.textObj:setTextColor (color)         row.textObj.anchorX = 0         row.textObj.x = 40         row.textObj.y = rowGroup.contentHeight \* 0.35                  rowGroup:insert(row.textObj)          end -- onRowRender          local function rowListener(event)          end -- rowListener          for x = 1, #listRecs do         list:insertRow {             onRender = onRowRender,             listener = rowListener         }     end      end -- showRecords setup() loadData() showRecords()  

unless I am making something wrong, but at this point in the video I see the last names

in my table … everything is empty

also he is using “mask”. I think in widget 2.0 there is no mask…

Please someone on the other side, help me with this, please…

thanks for all your time, honestly, thank you for everything

Victor

You set up a table say:

[lua]

local rowData = {}

rowData[1] = {name = “Nigel”, age = 35, sex = “M”, pic = “nigel.jpg”

rowData[2] = {name = “Alan”, age = 28, sex = “M”, pic = “alan.png”

rowData[3] = {name = “Gladys”, age = 81, sex = “F”, pic = “gladys.jpg”

[/lua]

Then in your onRowRender:

[lua]

local function onRowRender( event )

    – Get reference to the row group

    local row = event.row

      local id = row.index

    local rowHeight = row.contentHeight

    local rowWidth = row.contentWidth

    local t = display.newText( row, "Name: " …r owData[id] .n ame, 0 , 0 , nil, 12 )

      t.x = -rowWidth/ 2 + 10

    t.y = rowHeight * 0.5

      local t = display.newText( row, "Age: " …r owData[id] .a ge, 0 , 0 , nil, 12 )

      t.x = - 30

    t.y = rowHeight * 0.5

      local t = display.newText( row, "Sex: " …r owData[id] .s ex, 0 , 0 , nil, 12 )

      t.x = 30

    t.y = rowHeight * 0.5

      local i = display.newImageRect(row, rowData[id].pic, 32 , 32 )

      i.x = rowWidth/ 2 - 20

      i.y = rowHeight * 0.5

   

end

[/lua]

Thanks for the reply Nick

I got this Error…

I took the pictures out, and add the " } " at the end

local rowData = {} rowData[1] = {name = "Nigel", age = 35, sex = "M"} rowData[2] = {name = "Alan", age = 28, sex = "M"} rowData[3] = {name = "Gladys", age = 81, sex = "F"} -- ================================================== -- OOOOOOOO[onRowRender]OOOOOOOOOOOOOOO -- ==================================================== -- The "onRowRender" function may go here (see example under "Inserting Rows", above) local function onRowRender( event ) -- Get reference to the row group     local row = event.row       local id = row.index       local rowHeight = row.contentHeight     local rowWidth = row.contentWidth       local t = display.newText( row, "Name: "..rowData[id].name, 0, 0, native.systemFont, 12 )     t.x = -rowWidth/2 + 10     t.y = rowHeight \* 0.5       local t = display.newText( row, "Age: "..rowData[id].age, 0, 0, native.systemFont, 12 )     t.x = - 30     t.y = rowHeight \* 0.5       local t = display.newText( row, "Sex: "..rowData[id].sex, 0, 0, native.systemFont, 12 )     t.x = 30     t.y = rowHeight \* 0.5 end -- ===================================================== -- OOOOOOOOOOOOOOOO[END]OOOOOOOOOOOOOOOO -- =====================================================

And I got ERROR

File: information.lua
Line: 106
Attempt to index field ‘?’ (a nil value)

any idea?

Somehow I delete 1 line and it works! I don’t even know how…

I have a table

songList = { "Twinkle Twinkle Little Star", "Mary Had A Little Lamb", "Old McDonalds", "Baby Song", "The Wind", "Fly Me To The Sunshine", "The Happy Song" }

then I have a function [onRowRender]

-- The "onRowRender" function may go here (see example under "Inserting Rows", above) local function onRowRender( event )     --local phase = event.phase -- I don't know what is this for     local row = event.row -- Get reference to the row group              local nameOfSongs         if songList[row.index] ~= nil then           nameOfSongs = songList[row.index]         else           nameOfSongs = ""         end              -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added     local rowHeight = row.contentHeight     local rowWidth = row.contentWidth     local songName = display.newText( row, nameOfSongs, 0, 0, native.systemFont, 10 )     songName.anchorX = 0     songName.x = 40     songName.y = rowHeight \* 0.5     songName:setTextColor( 0, .12, .98 ) end

Then I create the table view

-- Create the widget local tableView = widget.newTableView {     left = 20,     top = 20,     height = 240,     width = 440,     onRowRender = onRowRender,     onRowTouch = onRowTouch,     backgroundColor = { .66, .65, .77 },     listener = scrollListener }     sceneGroup:insert(tableView)

And finally I make a loop to insert the rows

-- Insert as many rows as the table "rowText" for i = 1, #songList do     local isCategory = false     local rowHeight = 30     local rowColor = { default={ 1, 1, 1 }, over={ .12, .56, .56, .25 } }     local lineColor = { 0.5, 0.5, 0.5 }     tableView:insertRow( -- Insert a row into the tableView         {             isCategory = isCategory,             rowHeight = rowHeight,             rowColor = rowColor,             lineColor = lineColor         }     ) end

and that’s it – it works

But I still have a lot of questions…

1.- How do I add columns?

2.- How do I “touch” row3, and go to scene3

like this function

local function onRowTouch() audio.play(playDo) composer.gotoScene("home") end

if I touch row 7 got to scene 7, like that

Right now All the rows go to the same scene

3.- How do I change information later in the game…

if I am in scene 24 I would like to add code there

so when I come back to the table view scene

instead of Twinkle Twinkle, I would see “Great”

I wish there will be a class room, close to where I live,

in a college or high school

where I can actually have a teacher, and I can go and take

lessons, and ask in person, all these questions

is there actually lessons in person somewhere in the USA to learn Corona and lua?

Hi @helloworld2013d,

Where in the USA are you located? In several cities, there are Corona ambassadors and possibly even meet-ups where you could seek help on coding and development.

Brent

in California, 1 hr from San Diego and 2 Hrs from LA

in the City of Murrieta

helloworld2013d, there are a couple of active Meetup groups in California.

You might also consider books from Dr. Brian Burton

Dr. Burton is offering a 20% discount till the end of the year. Use coupon code CoronaGeek at checkout to save.

Here are also some excellent video courses that might be helpful.

Thank you. I sign up for the San Diego one.

Let’s see who I meet to learn more

You set up a table say:

[lua]

local rowData = {}

rowData[1] = {name = “Nigel”, age = 35, sex = “M”, pic = “nigel.jpg”

rowData[2] = {name = “Alan”, age = 28, sex = “M”, pic = “alan.png”

rowData[3] = {name = “Gladys”, age = 81, sex = “F”, pic = “gladys.jpg”

[/lua]

Then in your onRowRender:

[lua]

local function onRowRender( event )

    – Get reference to the row group

    local row = event.row

      local id = row.index

    local rowHeight = row.contentHeight

    local rowWidth = row.contentWidth

    local t = display.newText( row, "Name: " …r owData[id] .n ame, 0 , 0 , nil, 12 )

      t.x = -rowWidth/ 2 + 10

    t.y = rowHeight * 0.5

      local t = display.newText( row, "Age: " …r owData[id] .a ge, 0 , 0 , nil, 12 )

      t.x = - 30

    t.y = rowHeight * 0.5

      local t = display.newText( row, "Sex: " …r owData[id] .s ex, 0 , 0 , nil, 12 )

      t.x = 30

    t.y = rowHeight * 0.5

      local i = display.newImageRect(row, rowData[id].pic, 32 , 32 )

      i.x = rowWidth/ 2 - 20

      i.y = rowHeight * 0.5

   

end

[/lua]

Thanks for the reply Nick

I got this Error…

I took the pictures out, and add the " } " at the end

local rowData = {} rowData[1] = {name = "Nigel", age = 35, sex = "M"} rowData[2] = {name = "Alan", age = 28, sex = "M"} rowData[3] = {name = "Gladys", age = 81, sex = "F"} -- ================================================== -- OOOOOOOO[onRowRender]OOOOOOOOOOOOOOO -- ==================================================== -- The "onRowRender" function may go here (see example under "Inserting Rows", above) local function onRowRender( event ) -- Get reference to the row group     local row = event.row       local id = row.index       local rowHeight = row.contentHeight     local rowWidth = row.contentWidth       local t = display.newText( row, "Name: "..rowData[id].name, 0, 0, native.systemFont, 12 )     t.x = -rowWidth/2 + 10     t.y = rowHeight \* 0.5       local t = display.newText( row, "Age: "..rowData[id].age, 0, 0, native.systemFont, 12 )     t.x = - 30     t.y = rowHeight \* 0.5       local t = display.newText( row, "Sex: "..rowData[id].sex, 0, 0, native.systemFont, 12 )     t.x = 30     t.y = rowHeight \* 0.5 end -- ===================================================== -- OOOOOOOOOOOOOOOO[END]OOOOOOOOOOOOOOOO -- =====================================================

And I got ERROR

File: information.lua
Line: 106
Attempt to index field ‘?’ (a nil value)

any idea?

Somehow I delete 1 line and it works! I don’t even know how…

I have a table

songList = { "Twinkle Twinkle Little Star", "Mary Had A Little Lamb", "Old McDonalds", "Baby Song", "The Wind", "Fly Me To The Sunshine", "The Happy Song" }

then I have a function [onRowRender]

-- The "onRowRender" function may go here (see example under "Inserting Rows", above) local function onRowRender( event )     --local phase = event.phase -- I don't know what is this for     local row = event.row -- Get reference to the row group              local nameOfSongs         if songList[row.index] ~= nil then           nameOfSongs = songList[row.index]         else           nameOfSongs = ""         end              -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added     local rowHeight = row.contentHeight     local rowWidth = row.contentWidth     local songName = display.newText( row, nameOfSongs, 0, 0, native.systemFont, 10 )     songName.anchorX = 0     songName.x = 40     songName.y = rowHeight \* 0.5     songName:setTextColor( 0, .12, .98 ) end

Then I create the table view

-- Create the widget local tableView = widget.newTableView {     left = 20,     top = 20,     height = 240,     width = 440,     onRowRender = onRowRender,     onRowTouch = onRowTouch,     backgroundColor = { .66, .65, .77 },     listener = scrollListener }     sceneGroup:insert(tableView)

And finally I make a loop to insert the rows

-- Insert as many rows as the table "rowText" for i = 1, #songList do     local isCategory = false     local rowHeight = 30     local rowColor = { default={ 1, 1, 1 }, over={ .12, .56, .56, .25 } }     local lineColor = { 0.5, 0.5, 0.5 }     tableView:insertRow( -- Insert a row into the tableView         {             isCategory = isCategory,             rowHeight = rowHeight,             rowColor = rowColor,             lineColor = lineColor         }     ) end

and that’s it – it works

But I still have a lot of questions…

1.- How do I add columns?

2.- How do I “touch” row3, and go to scene3

like this function

local function onRowTouch() audio.play(playDo) composer.gotoScene("home") end

if I touch row 7 got to scene 7, like that

Right now All the rows go to the same scene

3.- How do I change information later in the game…

if I am in scene 24 I would like to add code there

so when I come back to the table view scene

instead of Twinkle Twinkle, I would see “Great”

I wish there will be a class room, close to where I live,

in a college or high school

where I can actually have a teacher, and I can go and take

lessons, and ask in person, all these questions

is there actually lessons in person somewhere in the USA to learn Corona and lua?

Hi @helloworld2013d,

Where in the USA are you located? In several cities, there are Corona ambassadors and possibly even meet-ups where you could seek help on coding and development.

Brent

in California, 1 hr from San Diego and 2 Hrs from LA

in the City of Murrieta

helloworld2013d, there are a couple of active Meetup groups in California.

You might also consider books from Dr. Brian Burton

Dr. Burton is offering a 20% discount till the end of the year. Use coupon code CoronaGeek at checkout to save.

Here are also some excellent video courses that might be helpful.

Thank you. I sign up for the San Diego one.

Let’s see who I meet to learn more