Where to place widget.newTableView in storybard??

I’m building an app where I use widget.newtabbar, widget.newtableview and storyboard.
The table is populated with data from an sql database situated in system.DocumentsDirectory.
I have a scene where you can add new data to the data base.

The Problem:
After adding new data I go back to the the tableview, but it is not showing a new row with new data. When I restart the app the new row shows up. The table is placed in “function scene: createScene()”

I have triede to move the table to “function scene: enterScene()”. Now the row is added instantly, but scrollview and OnROwTouch stops to work.

Has anyone experienced this? [import]uid: 119838 topic_id: 25422 reply_id: 325422[/import]

createScene() is where stuff gets built once and held in memory until the scene is purged (by choice or by memory limitations).

enterScene() is where stuff happens every-time you enter.

If you want your table to update with new rows every time you go back to the scene, then you either need to build the tableView in enterScene() or write a function to use tableView:insertRow(). (ie: check to see if the rows match your data, if not, insertRows())

There’s nothing wrong with making your table in enterScene() so I’d need to know more about your exact problem with scrollView and onRowTouch…
[import]uid: 41884 topic_id: 25422 reply_id: 102716[/import]

thank you for answering Richard9

I have posted my code for my table in enterScene() and beneath this my code fro inserting data. They are both in separate lua files.

I have only worked with Corona since January and is quite new to programming, so I have tried but with out luck to write a function for updating the table. Do I have to insert the whole table once more? I’m not sure how I can write the function for updating the table.

EnterScene code:

[code]


– scenetemplate.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local widget = require “widget”
widget.setTheme( “theme_ios” )

local sqlite = require “sqlite3”

– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

local path = system.pathForFile( “colordata.db”, system.DocumentsDirectory )
local db = sqlite3.open( path )

local tablesetup = [[CREATE TABLE IF NOT EXISTS list (id INTEGER PRIMARY KEY autoincrement, name, producer);]]
–db:exec( tablesetup )

local listOptions = {
top = 60,
width= 320,
height = 366,
maskFile = “mask-410.png”

}

local list = widget.newTableView (listOptions)
for row in db:nrows[[SELECT * FROM list]] do

local producer = row.producer
local name = row.name

local function onRowTouch( event )
local row = event.row
local text = rowtextObj
local background = event.background

if event.phase == “press” then

print( "Pressed row: " … event.index )
background:setFillColor( 255, 255, 255, 255 )

if row.textObj then
row.textObj:setReferencePoint( display.TopLeftReferencePoint )
row.textObj.x = 20
end

elseif event.phase == “release” or event.phase == “tap” then

print( "Tapped and/or Released row: " … event.index )
background:setFillColor( 0, 110, 233, 255 )
row.reRender = true

– set chosen row index to this row’s index
producerP = producer
nameP = name

– go to row scene
storyboard.gotoScene( “colorPage”, “slideLeft”, 350 )
end

end
– function below handles row rendering
local function onRowRender( event )
local group = event.view
local row = event.target
local index = event.index
local id = event.id
local bg = display.newImageRect(“rowBgNew.png”, 320, 40)
bg:setReferencePoint( display.CenterReferencePoint )
bg.x = row.width * 0.5
bg.y = row.height * 0.5
local text = display.newText( name… ", "…producer, 0, 0, native.systemFont, 12)
text:setReferencePoint( display.CenterLeftReferencePoint )
text:setTextColor(76, 48, 30 )
text.x = 25
text.y = row.height * 0.5

group:insert( bg )
group:insert( text )

row.textObj = text
end

list:insertRow{
–id = "unique-row " …i,
onRender=onRowRender,
listener = onRowTouch,
height = 39

}

end

group:insert( list )

–tab bar

local sbHeight = display.statusBarHeight
local tbHeight = 44
local top = sbHeight + tbHeight

–top bar
local titleBar = widget.newTabBar{
top = sbHeight,
background = “navBarNew.png”,
height = 44
}
group:insert( titleBar )

local shadow = display.newImage( “assets/shadow.png” )
shadow:setReferencePoint( display.TopLeftReferencePoint)
shadow.x, shadow.y = 0, top
shadow.xScale = 320 / shadow.contentWidth
shadow.alpha = 0,45
group:insert( shadow )

local titleTextPigment = display.newImage(“colorTitel.png”)
titleTextPigment.x = 160
titleTextPigment.y = titleBar.y
group:insert( titleTextPigment)

end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view


– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)


end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view


– INSERT code here (e.g. remove listeners, widgets, save state, etc.)


end

function scene:destoryScene( event )
local group = self.view


– INSERT code here (e.g. remove listeners, widgets, save state, etc.)


end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
–scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene)

return scene

[code]

Insert Data scene:

[code]


– scenetemplate.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene(pigmentList)
local widget = require “widget”
widget.setTheme( “theme_ios” )
local sqlite = require “sqlite3”

local titel1
local addColorButton



– NOTE:

– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.



– BEGINNING OF YOUR IMPLEMENTATION

local testText
local titleTextPigment
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
local path = system.pathForFile(“colordata.db”, system.DocumentsDirectory)
db = sqlite3.open( path )
print(path)

–add function for button add color eller save
local addColor_function = function ( event )
if event.phase == “release” then

– save color data to database
local tablefill =“INSERT INTO list VALUES (NULL,’” … name.text … “’,’” … producer.text … “’);”
print(tablefill)
db:exec( tablefill )

–return to color list
native.setKeyboardFocus( nil )

storyboard.gotoScene( “favorit”, “slideRight”, 350 )
storyboard.removeScene( “pigmentList” )

end

end
–objects
local bkg = display.newImage( “bkg.png” )
bkg:setReferencePoint( display.CenterReferencePoint )
bkg.x = 160
bkg.y = 240
group:insert( bkg )

addColorButton = widget.newButton {
label = “Add Color”,
onEvent = addColor_function
}

addColorButton.x = 160
addColorButton.y = 120

group:insert(addColorButton)

titel1 = display.newEmbossedText( “ADD A COLOR”, 0 , 0, native.systemFontBold, 20 )
titel1:setTextColor( 0 )
titel1:setReferencePoint( display.CenterReferencePoint )
titel1.x, titel1.y = 160 , 80
titel1.isVisible = true

group:insert( titel1 )

– textboxes and labels

local nameLabel = display.newText(“Name:”, 20, 155, native.systemFontBold, 12)
nameLabel:setTextColor(0)
group: insert(nameLabel)

name = native.newTextField(100, 155, 150, 22)
name.inputType=“default”
name.isVisible = true
local producerLabel = display.newText(“Producer:”, 20, 180, native.systemFontBold, 12 )
producerLabel:setTextColor(0)
group: insert(producerLabel)

producer = native.newTextField(100, 180, 150, 22)
producer.inputType=“default”
producer.isVisible = true
–staus bar
local sbHeight = display.statusBarHeight
local tbHeight = 44
local top = sbHeight + tbHeight

–top bar
local titleBar = widget.newTabBar{
top = sbHeight,
background = “navBarNew.png”,
height = 44
}
group:insert( titleBar )

local shadow = display.newImage( “assets/shadow.png” )
shadow:setReferencePoint( display.TopLeftReferencePoint)
shadow.x, shadow.y = 0, top
shadow.xScale = 320 / shadow.contentWidth
shadow.alpha = 0,45
group:insert( shadow )

local titleTextPigment = display.newImage(“pigmentTitel.png”)
titleTextPigment.x = 160
titleTextPigment.y = titleBar.y
group:insert( titleTextPigment)

end

– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view


– INSERT code here (e.g. start timers, load audio, start listeners, etc.)


if name then
name.isVisible = true
end

if producer then
producer.isVisible = true
end

end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view


– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)


if name then
name.isVisible = false
end

if producer then
producer.isVisible = false
end

end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view

end


– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene

[code] [import]uid: 119838 topic_id: 25422 reply_id: 102793[/import]

(Just a tip; try not to paste everything in the forums; we just want to know about particular function problems or areas…)

The last time I attempted to build a table using the widget (a year ago, basically, before the current implementation), yeah, I basically would make a function called refresh() that would:

a. Record important info (basically just the x/y position) of the table
b. Delete the entire table
c. Create the entire table again

…which can actually happen almost instantaneously depending on the amount of data or steps involved. But if you know that you’re just adding (and not subtracting or substantially reordering) you could use the insertRow() function? (From the API page:)

[code]tableView:insertRow( [params] )
This method is used for inserting rows into the tableView (see example for usage demonstration). This function accepts the following parameters:

id (string, can be used to identify row)
width, height (numbers)
rowColor, lineColor (tables with r, g, b, a values)
isCategory (boolean, true/false - will cause row to “stick” to top of tableView)
listener build 2012.721 (function - see Row Events section)
onEvent (function - see Row Events section)
onRender (function - see Row Events section)[/code] [import]uid: 41884 topic_id: 25422 reply_id: 102807[/import]

Thank you for pointing me, in the right direction. I cleaned up the code and moved the table to enterScene, and now everything is working. And. sorry about the clumsy code, won’t happen again ;0)

Now I only have one other problem that I’m trying to solve:
If I go from table to detail scene, and leave the detail scene via the tabbar, when I return to the table via the tab it shows a black screen.

If I can’t make it work I will post a new thread. [import]uid: 119838 topic_id: 25422 reply_id: 103030[/import]

the same thing happens in the widget Demo. Maybe it is a bug? [import]uid: 119838 topic_id: 25422 reply_id: 103031[/import]

What version are you using, and are you using transitions? (Various severe transition bugs just got fixed in the last couple of builds) [import]uid: 41884 topic_id: 25422 reply_id: 103294[/import]

@richard9:
You said you created a refresh() function that would:

“a. Record important info (basically just the x/y position) of the table
b. Delete the entire table
c. Create the entire table again”

Would be able to share that?
Inparticular I’m interested in it being called from within the OnTouch function fo the same tableview.

Any help would be appreciated.
Thanks
Suhada [import]uid: 67842 topic_id: 25422 reply_id: 103454[/import]

I’ll look at my code but the problem is that I was using widget.newTableView() back in the 500-600 series, before the current architecture with rowRenderers. (I literally stopped the project due to the instability of widgets back then; they’re much better now. Though I still want that scroll bar, Beebe!) :slight_smile:

To make something similar work here, maybe create a function and package all of the tableView functions inside of it?

[code]local listOptions = {} --your settings
local list – predeclare your list variable

local function refresh()
if list ~= nil then list:removeSelf() end
list = widget.newTableView( listOptions )

local function onRowTouch( event )
–row touch stuff
end

local function onRowRender( event )
–row render stuff
end

for i = 1, 100 do
–row iterator stuff
end
end

refresh() – will both create initial list or wipe and rebuild
[/code]

I haven’t tested that yet though. It sounds like it should work in practice but you would need to experiment to see where the iterator needs to live in scope.

(My current project works a bit different; my tableViews are located in seperate scenes, and I use storyboard.purgeScene() prior to loading, which forces the entire scene to be recreated, hence refreshing the table. But obviously that only works in a few types of apps.) [import]uid: 41884 topic_id: 25422 reply_id: 103589[/import]