Tableview widget functions, but renders no results

In aiming to use the tableview widget, I have copied the code from here,

http://coronalabs.com/blog/2014/03/04/tutorial-advanced-tableview-tactics/

and used this tutorial here from Corona,

https://www.youtube.com/watch?v=PXo4Rv5KPjo

But in both instances I get an empty table with lines, and that is all. Here is the code I was using in the latest instance,

local widget = require( "widget" ) require ("sqlite3") local path = system.pathForFile("data.db", system.DocumentsDirectory) local db = sqlite3.open( path ) local table\_options = { top = 0, onRowRender = onRowRender, } local tableView = widget.newTableView (table\_options) for row in db:nrows("SELECT \* FROM city LIMIT 8") do local rowParams = { ID = row.id, Name = row.city } tableView:insertRow ( { rowHeight = 50, params = rowParams, } ) end local function onRowRender (event) local row = event.row local font = native.systemFont local fontSize = 24 local rowHeight = row.height / 2 local options\_id = { parent = row, text = row.params.ID, x = 50, y = rowHeight, font = font, fontSize = fontSize } row.id = display.NewText (options\_id) row.id.anchorX = 0 row.id:setFillColor(1, 1, 1) local options\_name = { parent = row, text = row.params.Name, x = 100, y = rowHeight, font = font, fontSize = fontSize } row.name = display.NewText (options\_city) row.name.anchorX = 0 row.name:setFillColor(0, 0, 0) row:insert( row.ID ) row:insert( row.Name ) end

Hi @Betlic,

I see various syntax/scope issues here.

  1. First, you are not assigning your “sqlite3” require() to anything. It should be this:

[lua]

local sqlite3 = require( “sqlite3” )

[/lua]

  1. Your “onRowRender()” reference is out of scope. In the “table_options” table, you say this…

[lua]

onRowRender = onRowRender,

[/lua]

…however, you don’t actually create that function until later down, and because it’s local, Lua does not know anything about it when you do the initial call.

Hope this helps,

Brent

Hello,

I repaired your first suggestion, but Im not sure where onRowRender = onRowRender, would go.

Probably the easiest thing would be to move the entire “onRowRender” function above the line where you create the table view.

Hm I must still be missing something. I did check to make sure there was data, there is. I also did a print, it does print out rows. 

Though I did notice I can edit these row names in this section.,

local rowParams = { ID = row.id, Name = row.city }

and it does not error, or report it cant find these names, so it must not be reading it.

Heres how I moved it around.

local widget = require( "widget" ) local sqlite3 = require( "sqlite3" ) local path = system.pathForFile("data.db", system.DocumentsDirectory) local db = sqlite3.open( path ) local table\_options = { top = 0, onRowRender = onRowRender, } local function onRowRender (event) local row = event.row local font = native.systemFont local fontSize = 24 local rowHeight = row.height / 2 local options\_id = { parent = row, text = row.params.ID, x = 50, y = rowHeight, font = font, fontSize = fontSize } row.id = display.NewText (options\_id) row.id.anchorX = 0 row.id:setFillColor(1, 1, 1) local options\_name = { parent = row, text = row.params.Name, x = 100, y = rowHeight, font = font, fontSize = fontSize } row.name = display.NewText (options\_city) row.name.anchorX = 0 row.name:setFillColor(0, 0, 0) end local tableView = widget.newTableView (table\_options) for row in db:nrows("SELECT \* FROM city LIMIT 8") do print (row.city) local rowParams = { ID = row.id, Name = row.city } tableView:insertRow ( { rowHeight = 50, params = rowParams, } ) end

Hi Betlic,

This is still a scope issue. Inspect these lines…

[lua]

local table_options = {

    top = 0,

    onRowRender = onRowRender,

}

local function onRowRender (event)

[/lua]

… and you will see, in particular:

[lua]

    onRowRender = onRowRender

[/lua]

So, you are telling the property within “table_options” to be equal to (=) “onRowRender”, which refers to the function you’ve written below, but Lua has no idea what that function is, because you haven’t scoped it properly. At the time when Lua sees this line (onRowRender = onRowRender), that variable is nil, and so Lua never even knows about your function later, for example when you tell the table view to render its rows.

Best regards,

Brent

Thanks I went back to look at it, and this time followed the documentation here, and just replaced   ( row, "Row " … row.index, 0, 0, nil, 14 )    with the right values and it worked.

Hi @Betlic,

I see various syntax/scope issues here.

  1. First, you are not assigning your “sqlite3” require() to anything. It should be this:

[lua]

local sqlite3 = require( “sqlite3” )

[/lua]

  1. Your “onRowRender()” reference is out of scope. In the “table_options” table, you say this…

[lua]

onRowRender = onRowRender,

[/lua]

…however, you don’t actually create that function until later down, and because it’s local, Lua does not know anything about it when you do the initial call.

Hope this helps,

Brent

Hello,

I repaired your first suggestion, but Im not sure where onRowRender = onRowRender, would go.

Probably the easiest thing would be to move the entire “onRowRender” function above the line where you create the table view.

Hm I must still be missing something. I did check to make sure there was data, there is. I also did a print, it does print out rows. 

Though I did notice I can edit these row names in this section.,

local rowParams = { ID = row.id, Name = row.city }

and it does not error, or report it cant find these names, so it must not be reading it.

Heres how I moved it around.

local widget = require( "widget" ) local sqlite3 = require( "sqlite3" ) local path = system.pathForFile("data.db", system.DocumentsDirectory) local db = sqlite3.open( path ) local table\_options = { top = 0, onRowRender = onRowRender, } local function onRowRender (event) local row = event.row local font = native.systemFont local fontSize = 24 local rowHeight = row.height / 2 local options\_id = { parent = row, text = row.params.ID, x = 50, y = rowHeight, font = font, fontSize = fontSize } row.id = display.NewText (options\_id) row.id.anchorX = 0 row.id:setFillColor(1, 1, 1) local options\_name = { parent = row, text = row.params.Name, x = 100, y = rowHeight, font = font, fontSize = fontSize } row.name = display.NewText (options\_city) row.name.anchorX = 0 row.name:setFillColor(0, 0, 0) end local tableView = widget.newTableView (table\_options) for row in db:nrows("SELECT \* FROM city LIMIT 8") do print (row.city) local rowParams = { ID = row.id, Name = row.city } tableView:insertRow ( { rowHeight = 50, params = rowParams, } ) end

Hi Betlic,

This is still a scope issue. Inspect these lines…

[lua]

local table_options = {

    top = 0,

    onRowRender = onRowRender,

}

local function onRowRender (event)

[/lua]

… and you will see, in particular:

[lua]

    onRowRender = onRowRender

[/lua]

So, you are telling the property within “table_options” to be equal to (=) “onRowRender”, which refers to the function you’ve written below, but Lua has no idea what that function is, because you haven’t scoped it properly. At the time when Lua sees this line (onRowRender = onRowRender), that variable is nil, and so Lua never even knows about your function later, for example when you tell the table view to render its rows.

Best regards,

Brent

Thanks I went back to look at it, and this time followed the documentation here, and just replaced   ( row, "Row " … row.index, 0, 0, nil, 14 )    with the right values and it worked.