I'm using a TableView widget, which creates a one-column table. My onRowRender() function is identical to the example, but when I run my app, the text for every row is stuck in the same spot and doesn't move with the table. Even if I change the rowTitle.y property's value to row.y, which puts the text in the correct starting position, it doesn't move when I scroll the table.
Here is the example code I'm working from, which comes from Corona's documentation and is also in my textbook:
local widget = require( "widget" )
-- Listen for tableView events
local function tableViewListener( event )
local phase = event.phase
print( event.phase )
end
-- Handle row rendering
local function onRowRender( event )
local phase = event.phase
local row = event.row
local rowTitle = display.newText( row, "Row " .. row.index, 0, 0, nil, 14 )
rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )
rowTitle.y = row.contentHeight * 0.5
rowTitle:setTextColor( 0, 0, 0 )
end
-- Handle row's becoming visible on screen
local function onRowUpdate( event )
local row = event.row
print( "Row:", row.index, " is now visible" )
end
-- Handle touches on the row
local function onRowTouch( event )
local phase = event.phase
if "press" == phase then
print( "Touched row:", event.target.index )
end
end
-- Create a tableView
local tableView = widget.newTableView
{
top = 100,
width = 320,
height = 510,
maskFile = "mask-410.png",
listener = tableViewListener,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}
-- Create 100 rows
for i = 1, 100 do
local isCategory = false
local rowHeight = 40
local rowColor =
{
default = { 255, 255, 255 },
}
local lineColor = { 220, 220, 220 }
-- Make some rows categories
if i == 25 or i == 50 or i == 75 then
isCategory = true
rowHeight = 24
rowColor =
{
default = { 150, 160, 180, 200 },
}
end
-- Insert the row into the tableView
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
end
-- delete the tenth row in the tableView
tableView:deleteRow( 10 )
And here is my code:
local widget = require "widget"
display.setStatusBar(display.HiddenStatusBar)
local availableSections = {"Intro", "Verse", "Chorus", "Verse", "Chorus", "Interlude", "Breakdown", "Verse", "Chorus", "Outro"}
--listen for tableView press events
local function tableViewListener( event )
local phase = event.phase
local row = event.target
print(event.phase)
end
--render rows
local function onRowRender( event )
local phase = event.phase
local row = event.row
local rowTitle = display.newText(availableSections[row.index], 0, 0, system.nativeFont, 24)
rowTitle.x = row.x - (row.contentWidth * 0.5) + (rowTitle.contentWidth * 0.5) + 30
rowTitle.y = row.contentHeight * 0.5
rowTitle:setTextColor(1, 1, 1)
end
--handle row touches
local function onRowTouch( event )
local phase = event.phase
local row = event.target
print("Row " .. row.index .. " touched.")
if phase == "press" then
print("Touched row:", event.target.index)
end
end
-- Create a tableView
local list = widget.newTableView
{
width = display.contentWidth,
height = display.contentHeight,
--maskFile = "mask-410.png",
listener = tableViewListener,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}
-- Create rows
for i = 1, table.getn(availableSections) do
local isCategory = false
local rowHeight = display.contentHeight/table.getn(availableSections)
local rowColor =
{
default = { 255, 255, 255 },
}
local lineColor = { 220, 220, 220 }
-- Insert the row into the tableView
list:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
end
Is there something I'm missing? Why would the text remain where it is while the table rows are scrollable?
You have to insert your objects into the row group.
row:insert(rowTitle)
Related
struggling with a problem! I need to insert in a tableview the outcome of a DB query. The problem is that i cannot insert data since when i do it the result is nil...If i try to insert fixed data (like a general string "hello") it works! Can someone help me? Thanks a lot.
function listatornei()
local dataURL = "http://www.lineitup.it/torneiopen.php?me="..usrUsr
numtornei=0
local function listaListener(event)
if event.phase=="ended" then
if (event.isError) then
print("Connection not available")
else
id_tor = {} -- array to store query field1
usr_tor = {} -- array to store query field2
local tab = event.response -- read json data
tab = json.decode( tab ) -- tabel conversion
if #tab > 0 then
for i=1, #tab do
id_tor[i] = tab[i]["idt"]
usr_tor[i] = tab[i]["gioc1_usr"]
numtornei=i
end
-- Listen for tableView events
local function tableViewListener( event )
local phase = event.phase
print( event.phase )
end
-- Handle row rendering
local function onRowRender( event )
local phase = event.phase
local row = event.row
local id = row.index
row.bg = display.newRect( 0, 0, display.contentWidth, 60 )
row.bg.anchorX = 0
row.bg.anchorY = 0
row.bg:setFillColor( 1, 1, 0 )
row:insert( row.bg )
row.idt = display.newText(id_tor[i], 0, 0, native.systemFontBold, 40 )
--row.idt = display.newText("pippo", 0, 0, native.systemFontBold, 40 )
row.idt.anchorX = 0
row.idt.anchorY = 0.5
row.idt:setFillColor( 0 )
row.idt.y = 20
row.idt.x = 42
row.usr = display.newText( usr_tor[i], 0, 0, native.systemFont, 40 )
--row.usr = display.newText( "pluto", 0, 0, native.systemFont, 40 )
row.usr.anchorX = 0
row.usr.anchorY = 0.5
row.usr:setFillColor( 0.5 )
row.usr.y = 20
row.usr.x = 250
row:insert( row.idt )
row:insert( row.usr )
end
-- Handle row's becoming visible on screen
local function onRowUpdate( event )
local row = event.row
print( "Row:", row.index, " is now visible" )
end
-- Handle touches on the row
local function onRowTouch( event )
local phase = event.phase
if "press" == phase then
print( "Touched row:", event.target.index )
end
end
-- Create a tableView
local tableView = widget.newTableView
{
top = 100,
width = 620,
height = 410,
--maskFile = "formlog1.png",
listener = tableViewListener,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
}
for i = 1, numtornei do
local isCategory = false
local rowHeight = 60
local rowColor =
{
default = { 255, 255, 0 },
}
local lineColor = { 220, 220, 220 }
-- Make some rows categories
if i == 25 or i == 50 then
isCategory = true
rowHeight = 24
rowColor =
{
default = { 150, 160, 180, 200 },
}
end
-- Insert the row into the tableView
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
}
end
end
end
end
end
network.request( dataURL, "GET", listaListener )
end
You need to check your data coming back from the database. If the data from the database is nil, don't insert it. In your onRowRender function, you should test each bit of data to make sure it's not nil before you try to use to create an object.
In addition, you have a potential scope problem as well. onRowRender() is driven by an event of a table row becoming visible on the screen. It is not called synchronously inside of the for loop. The loop with your inserts will likely complete before the first row's onRowRender() completes. You also referencing the variable "i" inside onRowRender() which will be nil later when the rows render. If you database rows will always be in a 1 to 1 relationship with your tableView rows (i.e. no category rows), then you can use the row ID to look up your data in your data tables. If not, you should use the form of passing in data when you insert the rows. See: https://coronalabs.com/blog/2014/03/04/tutorial-advanced-tableview-tactics/ and look at the Passable Parameters section.
I'm trying to render the match results (string.find) in to a Row, with kinda works... but its only displaying the last match. so if I match 'jan' and 'kevin' it will; only list 'kevin'
Is there a way to fix this ?
code:
local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}
for i=1,#MasterData.child do
XMLdataTEST[i] = MasterData.child[i]
end
inputNumber = 1
check1 = 'jan'
check2 = 'kevin'
for i=1,#XMLdataTEST do
local data1 = XMLdata[i].child[1].value
local data2 = XMLdata[i].child[2].value
local data3 = XMLdata[i].child[3].value
local data4 = XMLdata[i].child[4].value
input1 = string.lower( data1.. "" )
input2 = string.lower(_G['check' .. inputNumber] )
input = input2
if string.find( input1.. "" , input ) then
print(inputNumber.. " match with " ..input)
inputNumber = inputNumber + 1
local function onRowRender( event )
local row = event.row
local number = display.newText( row, "" .. row.index .. " - ", 12, 0, nil, 18 )
number:setReferencePoint( display.CenterLeftReferencePoint )
number.x = 15
number.y = row.height * 0.5
number:setFillColor( 0, 0, 0 )
local name = display.newText(row, input1, 12, 0, nil, 18 )
name:setReferencePoint( display.CenterLeftReferencePoint )
name.x = number.x + number.contentWidth
name.y = row.height * 0.5
name:setFillColor( 0, 0, 0 )
local score = display.newText(row,"testy", 12, 0, nil, 18 )
score:setReferencePoint( display.CenterLeftReferencePoint )
score.x = display.contentWidth - score.contentWidth - 20
score.y = row.height * 0.5
score:setFillColor( 0, 0, 0 )
end
local tableView = widget.newTableView
{
left = 0,
top = 0,
height = display.contentHeight,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
tableView.x = display.contentWidth + display.contentWidth/2 + 50
transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )
for i = 1, 1 do
local isCategory = false
local rowHeight = 40
local rowColor = { 255, 255, 255 }
local lineColor = { 220, 220, 220 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
onRender = onRowRender,
}
end
end
end
Your tableView being declared within the outer loop meant that the first instance of it would be transitioned over by the second instance. And a quick look over the docs indicates that each event.row allows an optional params table to include any data you may need to render the row.
local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}
for i=1,#MasterData.child do
XMLdataTEST[i] = MasterData.child[i]
end
-- ** moved from loop **
local function onRowRender( event )
local row = event.row
local number = display.newText( row, "" .. row.index .. " - ", 12, 0, nil, 18 )
number:setReferencePoint( display.CenterLeftReferencePoint )
number.x = 15
number.y = row.height * 0.5
number:setFillColor( 0, 0, 0 )
-- ** changed to use params table **
local name = display.newText(row, row.params.input1, 12, 0, nil, 18 )
name:setReferencePoint( display.CenterLeftReferencePoint )
name.x = number.x + number.contentWidth
name.y = row.height * 0.5
name:setFillColor( 0, 0, 0 )
local score = display.newText(row,"testy", 12, 0, nil, 18 )
score:setReferencePoint( display.CenterLeftReferencePoint )
score.x = display.contentWidth - score.contentWidth - 20
score.y = row.height * 0.5
score:setFillColor( 0, 0, 0 )
end
-- ** moved from loop **
local tableView = widget.newTableView {
left = 0,
top = 0,
height = display.contentHeight,
width = display.contentWidth,
onRowRender = onRowRender,
onRowTouch = onRowTouch,
listener = scrollListener
}
tableView.x = display.contentWidth + display.contentWidth/2 + 50
transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )
inputNumber = 1
check1 = 'jan'
check2 = 'kevin'
for i=1,#XMLdataTEST do
local data1 = XMLdata[i].child[1].value
local data2 = XMLdata[i].child[2].value
local data3 = XMLdata[i].child[3].value
local data4 = XMLdata[i].child[4].value
input1 = string.lower( data1.. "" )
input2 = string.lower(_G['check' .. inputNumber] )
input = input2
if string.find( input1.. "" , input ) then
print(inputNumber.. " match with " ..input)
inputNumber = inputNumber + 1
local isCategory = false
local rowHeight = 40
local rowColor = { 255, 255, 255 }
local lineColor = { 220, 220, 220 }
tableView:insertRow
{
isCategory = isCategory,
rowHeight = rowHeight,
rowColor = rowColor,
lineColor = lineColor,
-- ** pass input1 to onRowRender **
params = { input1 = input1 }
}
end
end
im making a questions app that when you pick the correct answer button a +1 is added to the score and -1 when selecting wrong answer,
How can I make buttons able to be pressed once, but then not pressable again after that? since score keeps adding if you keep pressing on the button!
this is button1:
local widget = require( "widget" )
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
minusScore()
print( "Button was pressed and released" )
end
end
local button1 = widget.newButton
{
width = 350,
height = 360,
left= 30,
top= 220,
defaultFile = "speakers.png",
overFile = "wrong.png",
--label = "button",
onEvent = handleButtonEvent
}
this is the score function..maybe theres a way that the score adds 1 then stops:
-------------------score------------------------
local score = 0
local scoreTxt = display.newText( "0", 0, 0, "Helvetica", 40 )
scoreTxt:setReferencePoint(display.TopLeftReferencePoint)
scoreTxt.x = display.screenOriginX + 700
scoreTxt.y = display.screenOriginY + 37
scoreTxt:setTextColor(2,2,2)
---------------------score added 10-----------------------------
function updateScore()
score = score + 1
_G.score = score
scoreTxt.text = string.format(" %d", score)
end
local scoretimer = timer.performWithDelay(1, updateScore,1)
---------------------score minus 1-----------------------------
function minusScore()
score = score - 1
_G.score = score
scoreTxt.text = string.format(" %d", score)
end
local scoretimer = timer.performWithDelay(1, minusScore,1)
You could do something like this:
local minusButtonPressed = false
local function handleButtonEvent( event )
if ( ( "ended" == event.phase ) and (minusButtonPressed == false) ) then
minusScore()
print( "Button was pressed and released" )
--disable the button
minusButtonPressed = true
end
end
I am designing an Odd One Out round for a game that I am developing, every time a button is pressed, the onButtonEvent function is supposed to check if the word selected is the odd one out and then refreshes the buttons with different words.
The words are updating and the value assigned to each button is updating, I have been printing it out to the console. I think the problem is when I assign right[num] = t.value the only values that go into the table are the first 3 values that were originally assigned to the buttons. Why is this not being updated?
local function onButtonEvent( event )
local t = event.target
--local phase = event.phase
if event.phase == "release" then
--num = num + 1
displayBtns()
right[num] = t.value
print(right[1])
print(right[2])
print(right[3])
print(right[4])
-- --s3 = table.concat(right)
--print(s3)
if right[num] == answers[num].right then
print("correct!")
elseif right[num] == answers[num].wrong then
-- t.alpha = 0
-- select.alpha = 1
print("incorrect!")
elseif right[num] == answers[num].wrong2 then
else
print("incorrect!")
end
num = num + 1
num2 = num2 + 1
s3 = ""
-- display.remove( btn3 )
-- btn3 = nil
-- display.remove( btn2 )
-- btn2 = nil
-- display.remove( btn1 )
-- btn1 = nil
end
--num = math.random(1,#t)
s3 = ""
end
function displayBtns()
btn3 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].right,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn3.value = answers[num].right
print(btn3.value)
btn2 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].wrong2,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn2.value = answers[num].wrong2
print(btn2.value)
btn1 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].wrong,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn1.value = answers[num].wrong
print(btn1.value)
print("----------")
--btn3.label = answers[num].right
-- p1Button = display.newImage("images/SinglePlayer_button.png", 90, 140)
-- p2Button = display.newImage("images/2Player_button.png", 90, 220)
-- p1Button.touch = onSceneTouch
-- p1Button:addEventListener("touch", p1Button)
-- p2Button.touch = onSceneTouch1
-- p2Button:addEventListener("touch", p2Button)
btn1.x = 90; btn1.y = 245
btn2.x = 240; btn2.y = 245
btn3.x = 390; btn3.y = 245
-- p2Button.x = 240; p2Button.y = 260
end
displayBtns()
The problem is probably here:
elseif right[num] == answers[num].wrong2 then
else
print("incorrect!")
end
You have nothing set in the elseif statement.
I have a menu lua and when i choose 1 of the category (the "songselect" lua) and go back to the menu lua i got this error:
Runtime error
...ocuments\corona projects\singingbeemenu\director.lua:151: attempt to
call field 'unloadMe' (a nil value)
stack traceback:
[C]: in function 'unloadMe'
...ocuments\corona projects\singingbeemenu\director.lua:151: in function
'_listener
'
this is 1 of my category the songselect.lua
module(..., package.seeall)
display.setStatusBar( display.HiddenStatusBar )
function new()
local localGroup = display.newGroup()
local tableView = require("tableView")
local ui = require("ui")
--------------------------------------------------------------------------
local screenOffsetW, screenOffsetH = display.contentWidth - display.viewableContentWidth, display.contentHeight - display.viewableContentHeight
local songList
local backBtn
local detailScreenText
local background = display.newRect(0, 0, display.contentWidth, display.contentHeight)
background:setFillColor(0, 0, 0)
local data = {}
--iPad: setup a color fill for selected items
local selected = display.newRect(0, 0, 50, 50) --add acolor fill to show the selected item
selected:setFillColor(67,141,241,180) --set the color fill to light blue
selected.isVisible = false --hide color fill until neede
-----------------------------------------------
data[1] = {}
data[1].title = "Crazy Song"
data[1].subtitle = "by Bruno Earth"
data[1].image = "note.png"
data[2] = {}
data[2].title = "Enter Sunman"
data[2].subtitle = "by Mentalica"
data[2].image = "note.png"
local topBoundary = display.screenOriginY + 40
local bottomBoundary = display.screenOriginY + 0
songList = tableView.newList{
data=data,
default="listItemBg.png",
over="listItemBg_over.png",
top=topBoundary,
bottom=bottomBoundary,
callback = function( row )
local g = display.newGroup()
local img = display.newImage(row.image)
g:insert(img)
img.x = math.floor(img.width*0.5 + 6)
img.y = math.floor(img.height*0.5)
local title = display.newText( row.title, 0, 0, native.systemFontBold, 16 )
title:setTextColor(0,0,0)
g:insert(title)
title.x = title.width*0.5 + img.width + 6
title.y = 30
local subtitle = display.newText( row.subtitle, 0, 0, native.systemFont, 14 )
subtitle:setTextColor(80,80,90)
g:insert(subtitle)
subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.y = title.y + title.height + 6
return g
end
}
local function scrollToTop()
songList:scrollTo(topBoundary-1)
end
local navBar = display.newImage("navBar.png")
navBar.x = display.contentWidth*.5
navBar.y = math.floor(display.screenOriginY + navBar.height*0.5)
local navHeader = display.newText("Song Lists", 0, 0, native.systemFontBold, 16)
navHeader:setTextColor(255, 255, 255)
navHeader.x = display.contentWidth*.5
navHeader.y = navBar.y
--Setup the back button
local backToMenu = function(event)
print (event.phase)
if event.phase == 'ended' then
print ("ok")
director:changeScene(event.target.scene, "fade")
print ("back!")
end
end
backBtn = display.newImage("backButton.png")
backBtn.x = math.floor(backBtn.width/2) + backBtn.width + screenOffsetW
backBtn.y = navBar.y
backBtn.scene = "menu"
backBtn:addEventListener("touch", backToMenu)
--backBtn.alpha = 0
local listBackground = display.newRect( 0, 0, songList.width, songList.height )
listBackground:setFillColor(255,255,255)
songList:insert(1,listBackground)
return localGroup
end
is it impossible the go back method in lua?
can anyone can help me and give an idea why i got the error?
thanks in advance...
I got this, i change my director.lua to the latest version...
Try this Lua file to solve your problem.